Skip to content

Commit 2e9f869

Browse files
justinfargnoliAidan
and
Aidan
authored
Reland "[LLVM] Add IRNormalizer Pass" (llvm#113780)
`IRNormalizer` will reorder instructions. Thus, we need to invalidate analyses. Done in cd500d2. This should resolve the [BuildBot failure](llvm#68176 (comment)). --- Original PR: llvm#68176 Original commit: 1295d2e Reverted with: 8a12e01 --- Add the llvm-canon tool. Description from the [original PR](https://reviews.llvm.org/D66029#change-wZv3yOpDdxIu): > Added a new llvm-canon tool which aims to transform LLVM Modules into a canonical form by reordering and renaming instructions while preserving the same semantics. This tool makes it easier to spot semantic differences while diffing two modules which have undergone different transformation passes. The current version of this tool can: - Reorder instructions within a function. - Rename instructions based on the operands. - Sort commutative operands. This code was originally written by @michalpaszkowski and [submitted to mainline LLVM](llvm@14d3585). However, it was quickly [reverted](llvm@335de55) to do BuildBot errors. Michal presented his version of the tool in [LLVM-Canon: Shooting for Clear Diffs](https://www.youtube.com/watch?v=c9WMijSOEUg). @AidanGoldfarb and I ported the code to the new pass manager, added more tests, and fixed some bugs related to PHI nodes that may have been the root cause of the BuildBot errors that caused the patch to be reverted. Additionally, we rewrote the implementation of instruction reordering to fix cases where the original algorithm would break use-def chains. Note that this is @AidanGoldfarb and I's first time submitting to LLVM. Please liberally critique the PR! CC @plotfi for initial review. --------- Co-authored-by: Aidan <[email protected]>
1 parent 0019565 commit 2e9f869

17 files changed

+1291
-1
lines changed

llvm/docs/Passes.rst

+8
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,14 @@ variables with initializers are marked as internal.
543543
An interprocedural variant of :ref:`Sparse Conditional Constant Propagation
544544
<passes-sccp>`.
545545

546+
``ir-normalizer``: Transforms IR into a normal form that's easier to diff
547+
----------------------------------------------------------------------------
548+
549+
This pass aims to transform LLVM Modules into a normal form by reordering and
550+
renaming instructions while preserving the same semantics. The normalizer makes
551+
it easier to spot semantic differences while diffing two modules which have
552+
undergone two different passes.
553+
546554
``jump-threading``: Jump Threading
547555
----------------------------------
548556

llvm/docs/ReleaseNotes.md

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ point (e.g. maybe you would like to give an example of the
4242
functionality, or simply have a lot to talk about), see the comment below
4343
for adding a new subsection. -->
4444

45+
* Added a new IRNormalizer pass which aims to transform LLVM modules into
46+
a normal form by reordering and renaming instructions while preserving the
47+
same semantics. The normalizer makes it easier to spot semantic differences
48+
when diffing two modules which have undergone different passes.
49+
4550
* ...
4651

4752
<!-- If you would like to document a larger change, then you can add a
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef LLVM_TRANSFORMS_UTILS_IRNORMALIZER_H
2+
#define LLVM_TRANSFORMS_UTILS_IRNORMALIZER_H
3+
4+
#include "llvm/IR/PassManager.h"
5+
6+
namespace llvm {
7+
8+
/// IRNormalizer aims to transform LLVM IR into normal form.
9+
struct IRNormalizerPass : public PassInfoMixin<IRNormalizerPass> {
10+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) const;
11+
};
12+
13+
} // namespace llvm
14+
15+
#endif // LLVM_TRANSFORMS_UTILS_IRNORMALIZER_H

llvm/lib/Passes/PassBuilder.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,9 @@
306306
#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
307307
#include "llvm/Transforms/Utils/FixIrreducible.h"
308308
#include "llvm/Transforms/Utils/HelloWorld.h"
309+
#include "llvm/Transforms/Utils/IRNormalizer.h"
309310
#include "llvm/Transforms/Utils/InjectTLIMappings.h"
310311
#include "llvm/Transforms/Utils/InstructionNamer.h"
311-
#include "llvm/Transforms/Utils/Instrumentation.h"
312312
#include "llvm/Transforms/Utils/LCSSA.h"
313313
#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
314314
#include "llvm/Transforms/Utils/LoopSimplify.h"

llvm/lib/Passes/PassRegistry.def

+1
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ FUNCTION_PASS("move-auto-init", MoveAutoInitPass())
416416
FUNCTION_PASS("nary-reassociate", NaryReassociatePass())
417417
FUNCTION_PASS("newgvn", NewGVNPass())
418418
FUNCTION_PASS("no-op-function", NoOpFunctionPass())
419+
FUNCTION_PASS("normalize", IRNormalizerPass())
419420
FUNCTION_PASS("objc-arc", ObjCARCOptPass())
420421
FUNCTION_PASS("objc-arc-contract", ObjCARCContractPass())
421422
FUNCTION_PASS("objc-arc-expand", ObjCARCExpandPass())

llvm/lib/Transforms/Utils/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ add_llvm_component_library(LLVMTransformUtils
3737
InstructionNamer.cpp
3838
Instrumentation.cpp
3939
IntegerDivision.cpp
40+
IRNormalizer.cpp
4041
LCSSA.cpp
4142
LibCallsShrinkWrap.cpp
4243
Local.cpp

0 commit comments

Comments
 (0)