[llvm] [EarlyCSE] Rematerialize alignment assumption. (PR #109131)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 19 12:47:32 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Florian Hahn (fhahn)
<details>
<summary>Changes</summary>
If the load has align metadata, preserve it via an alignment
assumption. Note that this doesn't use salavageKnowledge, as we need
to create the assumption for the value we replaced the load with.
Comparison run https://github.com/dtcxzyw/llvm-opt-benchmark/pull/1343#issuecomment-2361976028
Unfortunately the diff is too large to display, top changes
```
Top 5 improvements:
typst-rs/4p30esqzpn2o5olu.ll 6364278007 6347884002 -0.26%
llvm/StdLibraryFunctionsChecker.cpp.ll 25872800221 25821679537 -0.20%
meshlab/arap.cpp.ll 16171781140 16149344101 -0.14%
darktable/DngOpcodes.cpp.ll 2123576004 2120732832 -0.13%
hermes/Passes.cpp.ll 1997252275 1994672931 -0.13%
Top 5 regressions:
rust-analyzer-rs/4dj9fscdf5c509wz.ll 145086655 148887948 +2.62%
typst-rs/p1dgiootfedk7bo.ll 488966323 501623041 +2.59%
rust-analyzer-rs/1bjrygtvfxna7kin.ll 5561255586 5677108765 +2.08%
actix-rs/125u7gvq3v36sg70.ll 98217713 99929856 +1.74%
wasmtime-rs/3r0osxvwe4cd326n.ll 9141682789 9286492070 +1.58%
Overall: -0.00945255%
```
---
Full diff: https://github.com/llvm/llvm-project/pull/109131.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/Scalar/EarlyCSE.cpp (+13)
- (modified) llvm/test/Transforms/EarlyCSE/materialize-align-assumptions.ll (+3)
``````````diff
diff --git a/llvm/lib/Transforms/Scalar/EarlyCSE.cpp b/llvm/lib/Transforms/Scalar/EarlyCSE.cpp
index cf11f5bc885a75..743b333987bc9a 100644
--- a/llvm/lib/Transforms/Scalar/EarlyCSE.cpp
+++ b/llvm/lib/Transforms/Scalar/EarlyCSE.cpp
@@ -31,6 +31,7 @@
#include "llvm/IR/Constants.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
+#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
@@ -1588,6 +1589,18 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
if (InVal.IsLoad)
if (auto *I = dyn_cast<Instruction>(Op))
combineMetadataForCSE(I, &Inst, false);
+
+ // If the load has align metadata, preserve it via an alignment
+ // assumption. Note that this doesn't use salavageKnowledge, as we need
+ // to create the assumption for the value we replaced the load with.
+ if (auto *AlignMD = Inst.getMetadata(LLVMContext::MD_align)) {
+ auto *A = mdconst::extract<ConstantInt>(AlignMD->getOperand(0));
+ if (Op->getPointerAlignment(SQ.DL).value() % A->getZExtValue() != 0) {
+ IRBuilder B(&Inst);
+ B.CreateAlignmentAssumption(SQ.DL, Op, A);
+ }
+ }
+
if (!Inst.use_empty())
Inst.replaceAllUsesWith(Op);
salvageKnowledge(&Inst, &AC);
diff --git a/llvm/test/Transforms/EarlyCSE/materialize-align-assumptions.ll b/llvm/test/Transforms/EarlyCSE/materialize-align-assumptions.ll
index ea63376957162b..628577b0975071 100644
--- a/llvm/test/Transforms/EarlyCSE/materialize-align-assumptions.ll
+++ b/llvm/test/Transforms/EarlyCSE/materialize-align-assumptions.ll
@@ -10,6 +10,7 @@ define ptr @align_replacement_does_not_have_align_metadata(ptr noalias %p) {
; CHECK-NEXT: call void @foo(ptr [[L_1]])
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[L_1]], i64 4
; CHECK-NEXT: store ptr [[GEP]], ptr [[P]], align 8
+; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr [[GEP]], i64 4) ]
; CHECK-NEXT: ret ptr [[GEP]]
;
%l.1 = load ptr, ptr %p, align 8
@@ -27,6 +28,7 @@ define ptr @align_replacement_does_not_have_align_metadata2(ptr noalias %p) {
; CHECK-NEXT: [[L_1:%.*]] = load ptr, ptr [[P]], align 8
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[L_1]], i64 4
; CHECK-NEXT: store ptr [[GEP]], ptr [[P]], align 8
+; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr [[GEP]], i64 4) ]
; CHECK-NEXT: ret ptr [[GEP]]
;
%l.1 = load ptr, ptr %p, align 8
@@ -54,6 +56,7 @@ define ptr @align_replacement_has_smaller_alignment(ptr noalias %p) {
; CHECK-SAME: ptr noalias [[P:%.*]]) {
; CHECK-NEXT: [[L_1:%.*]] = load ptr, ptr [[P]], align 8, !align [[META0]]
; CHECK-NEXT: call void @foo(ptr [[L_1]])
+; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr [[L_1]], i64 8) ]
; CHECK-NEXT: ret ptr [[L_1]]
;
%l.1 = load ptr, ptr %p, align 8, !align !0
``````````
</details>
https://github.com/llvm/llvm-project/pull/109131
More information about the llvm-commits
mailing list