[llvm] [CGP] Undo constant propagation of pointers across calls (PR #102926)
Antonio Frighetto via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 28 01:52:13 PDT 2024
https://github.com/antoniofrighetto updated https://github.com/llvm/llvm-project/pull/102926
>From 2f610d985db48c85511683bac588cec6e8b1c42b Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Mon, 26 Aug 2024 22:02:59 +0200
Subject: [PATCH 1/2] [CGP] Introduce test for PR102926 (NFC)
---
...evert-constant-ptr-propagation-on-calls.ll | 152 ++++++++++++++++++
1 file changed, 152 insertions(+)
create mode 100644 llvm/test/Transforms/CodeGenPrepare/revert-constant-ptr-propagation-on-calls.ll
diff --git a/llvm/test/Transforms/CodeGenPrepare/revert-constant-ptr-propagation-on-calls.ll b/llvm/test/Transforms/CodeGenPrepare/revert-constant-ptr-propagation-on-calls.ll
new file mode 100644
index 00000000000000..61969044025b34
--- /dev/null
+++ b/llvm/test/Transforms/CodeGenPrepare/revert-constant-ptr-propagation-on-calls.ll
@@ -0,0 +1,152 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -passes='require<profile-summary>,function(codegenprepare)' -mtriple=aarch64-none-linux-gnu < %s | FileCheck %s
+
+%struct.S = type { i8 }
+%struct.X = type { i32 }
+
+ at g_getS = internal global %struct.S zeroinitializer, align 1
+ at g_getX = internal global %struct.X zeroinitializer, align 1
+ at guard = internal global i64 0, align 8
+
+declare ptr @getS_dec()
+declare extern_weak dllimport ptr @getS_dllimport_function()
+
+define ptr @getS() personality ptr @__gxx_personality_v0 {
+entry:
+ %guard = load atomic i8, ptr @guard acquire, align 8
+ %mask = and i8 %guard, 1
+ %cond = icmp eq i8 %mask, 0
+ br i1 %cond, label %to_be_init, label %return
+
+to_be_init: ; preds = %entry
+ %is_init = call i32 @__cxa_guard_acquire(ptr @guard)
+ %cond.2 = icmp ne i32 %is_init, 0
+ br i1 %cond.2, label %ctor, label %return
+
+ctor: ; preds = %to_be_init
+ invoke void @S_ctor(ptr @g_getS)
+ to label %continue unwind label %landing_pad
+
+continue: ; preds = %ctor
+ call void @__cxa_guard_release(ptr @guard)
+ br label %return
+
+return: ; preds = %continue, %to_be_init, %entry
+ ret ptr @g_getS
+
+landing_pad: ; preds = %ctor
+ %lp = landingpad { ptr, i32 } cleanup
+ call void @__cxa_guard_abort(ptr @guard)
+ resume { ptr, i32 } %lp
+}
+
+define ptr @getS_or_getX(i1 %cond) {
+entry:
+ %result = select i1 %cond, ptr @g_getS, ptr @g_getX
+ ret ptr %result
+}
+
+define weak ptr @getS_weak_function() {
+entry:
+ ret ptr @g_getS
+}
+
+; May revert propagation.
+define i32 @caller_1() {
+; CHECK-LABEL: @caller_1(
+; CHECK-NEXT: [[GETS_PTR:%.*]] = call ptr @getS()
+; CHECK-NEXT: [[GETI:%.*]] = call i32 @S_getI(ptr @g_getS)
+; CHECK-NEXT: ret i32 [[GETI]]
+;
+ %getS_ptr = call ptr @getS()
+ %getI = call i32 @S_getI(ptr @g_getS)
+ ret i32 %getI
+}
+
+; Cannot revert propagation due to use appearing in a different basic block.
+define i32 @caller_2() {
+; CHECK-LABEL: @caller_2(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[GETS_PTR:%.*]] = call ptr @getS()
+; CHECK-NEXT: br label [[USE:%.*]]
+; CHECK: use:
+; CHECK-NEXT: [[GETI:%.*]] = call i32 @S_getI(ptr [[GETS_PTR]])
+; CHECK-NEXT: ret i32 [[GETI]]
+;
+entry:
+ %getS_ptr = call ptr @getS()
+ br label %use
+
+use: ; preds = %entry
+ %getI = call i32 @S_getI(ptr %getS_ptr)
+ ret i32 %getI
+}
+
+; Cannot revert propagation due to use before call.
+define i32 @caller_3() {
+; CHECK-LABEL: @caller_3(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[GETI:%.*]] = call i32 @S_getI(ptr @g_getS)
+; CHECK-NEXT: [[GETS_PTR:%.*]] = call ptr @getS()
+; CHECK-NEXT: ret i32 [[GETI]]
+;
+entry:
+ %getI = call i32 @S_getI(ptr @g_getS)
+ %getS_ptr = call ptr @getS()
+ ret i32 %getI
+}
+
+; Cannot revert propagation due to non-uniform returned constant.
+define i32 @caller_4(i1 %cond) {
+; CHECK-LABEL: @caller_4(
+; CHECK-NEXT: [[GETS_OR_GETX_PTR:%.*]] = call ptr @getS_or_getX(i1 [[COND:%.*]])
+; CHECK-NEXT: [[GETI:%.*]] = call i32 @S_getI(ptr @g_getS)
+; CHECK-NEXT: ret i32 [[GETI]]
+;
+ %getS_or_getX_ptr = call ptr @getS_or_getX(i1 %cond)
+ %getI = call i32 @S_getI(ptr @g_getS)
+ ret i32 %getI
+}
+
+; Cannot revert propagation due to weak-linkage callee.
+define i32 @caller_5() {
+; CHECK-LABEL: @caller_5(
+; CHECK-NEXT: [[GETS_PTR:%.*]] = call ptr @getS_weak_function()
+; CHECK-NEXT: [[GETI:%.*]] = call i32 @S_getI(ptr @g_getS)
+; CHECK-NEXT: ret i32 [[GETI]]
+;
+ %getS_ptr = call ptr @getS_weak_function()
+ %getI = call i32 @S_getI(ptr @g_getS)
+ ret i32 %getI
+}
+
+; Cannot revert propagation due to callee with external function definition.
+define i32 @caller_6() {
+; CHECK-LABEL: @caller_6(
+; CHECK-NEXT: [[GETS_PTR:%.*]] = call ptr @getS_dec()
+; CHECK-NEXT: [[GETI:%.*]] = call i32 @S_getI(ptr @g_getS)
+; CHECK-NEXT: ret i32 [[GETI]]
+;
+ %getS_ptr = call ptr @getS_dec()
+ %getI = call i32 @S_getI(ptr @g_getS)
+ ret i32 %getI
+}
+
+; Cannot revert propagation due to callee with DLLImport storage class.
+define i32 @caller_7() {
+; CHECK-LABEL: @caller_7(
+; CHECK-NEXT: [[GETS_PTR:%.*]] = call ptr @getS_dllimport_function()
+; CHECK-NEXT: [[GETI:%.*]] = call i32 @S_getI(ptr @g_getS)
+; CHECK-NEXT: ret i32 [[GETI]]
+;
+ %getS_ptr = call ptr @getS_dllimport_function()
+ %getI = call i32 @S_getI(ptr @g_getS)
+ ret i32 %getI
+}
+
+declare i32 @__cxa_guard_acquire(ptr)
+declare void @S_ctor(ptr)
+declare i32 @S_getI(ptr)
+declare void @__cxa_guard_abort(ptr)
+declare void @__cxa_guard_release(ptr)
+declare i32 @__gxx_personality_v0(...)
>From 306654aab25e993894382fdb34729dacfcc3bdf5 Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Mon, 26 Aug 2024 22:03:11 +0200
Subject: [PATCH 2/2] [CGP] Undo constant propagation of pointers across calls
It may be profitable to revert SCCP propagation of C++ static values,
if such constants are pointers, in order to avoid redundant pointer
computation, since the method returning the constant is non-removable.
---
llvm/lib/CodeGen/CodeGenPrepare.cpp | 48 ++++++++++++++++++-
...evert-constant-ptr-propagation-on-calls.ll | 2 +-
2 files changed, 48 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index bf48c1fdab0ff0..fd5e4d80725c64 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -2671,7 +2671,8 @@ bool CodeGenPrepare::optimizeCallInst(CallInst *CI, ModifyDT &ModifiedDT) {
}
// From here on out we're working with named functions.
- if (!CI->getCalledFunction())
+ auto *Callee = CI->getCalledFunction();
+ if (!Callee)
return false;
// Lower all default uses of _chk calls. This is very similar
@@ -2686,6 +2687,51 @@ bool CodeGenPrepare::optimizeCallInst(CallInst *CI, ModifyDT &ModifiedDT) {
return true;
}
+ // SCCP may have propagated, among other things, C++ static variables across
+ // calls. If this happens to be the case, we may want to undo it in order to
+ // avoid redundant pointer computation of the constant, as the function method
+ // returning the constant needs to be executed anyways.
+ auto GetUniformReturnValue = [](const Function *F) -> Constant * {
+ if (!F->getReturnType()->isPointerTy())
+ return nullptr;
+
+ Constant *UniformValue = nullptr;
+ for (auto &BB : *F) {
+ if (auto *RI = dyn_cast<ReturnInst>(BB.getTerminator())) {
+ if (auto *V = dyn_cast<GlobalVariable>(RI->getReturnValue())) {
+ if (!UniformValue)
+ UniformValue = V;
+ else if (V != UniformValue)
+ return nullptr;
+ } else {
+ return nullptr;
+ }
+ }
+ }
+
+ return UniformValue;
+ };
+
+ if (!Callee->isInterposable()) {
+ if (Constant *RV = GetUniformReturnValue(Callee)) {
+ bool MadeChange = false;
+ for (Use &U : make_early_inc_range(RV->uses())) {
+ auto *I = dyn_cast<Instruction>(U.getUser());
+ if (!I || I->getParent() != CI->getParent()) {
+ // Limit to the same basic block to avoid extending the call-site live
+ // range, which otherwise could increase register pressure.
+ continue;
+ }
+ if (CI->comesBefore(I)) {
+ U.set(CI);
+ MadeChange = true;
+ }
+ }
+
+ return MadeChange;
+ }
+ }
+
return false;
}
diff --git a/llvm/test/Transforms/CodeGenPrepare/revert-constant-ptr-propagation-on-calls.ll b/llvm/test/Transforms/CodeGenPrepare/revert-constant-ptr-propagation-on-calls.ll
index 61969044025b34..0f381ce79e22b6 100644
--- a/llvm/test/Transforms/CodeGenPrepare/revert-constant-ptr-propagation-on-calls.ll
+++ b/llvm/test/Transforms/CodeGenPrepare/revert-constant-ptr-propagation-on-calls.ll
@@ -55,7 +55,7 @@ entry:
define i32 @caller_1() {
; CHECK-LABEL: @caller_1(
; CHECK-NEXT: [[GETS_PTR:%.*]] = call ptr @getS()
-; CHECK-NEXT: [[GETI:%.*]] = call i32 @S_getI(ptr @g_getS)
+; CHECK-NEXT: [[GETI:%.*]] = call i32 @S_getI(ptr [[GETS_PTR]])
; CHECK-NEXT: ret i32 [[GETI]]
;
%getS_ptr = call ptr @getS()
More information about the llvm-commits
mailing list