[PATCH] D109823: [GlobalOpt] Do not shrink global to bool for an unfavorable AS
Christudasan Devadasan via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 15 10:56:16 PDT 2021
cdevadas updated this revision to Diff 372751.
cdevadas added a comment.
Fixed the lit test to have only one check pattern.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D109823/new/
https://reviews.llvm.org/D109823
Files:
llvm/lib/Transforms/IPO/GlobalOpt.cpp
llvm/test/Transforms/GlobalOpt/shrink-global-to-bool.ll
Index: llvm/test/Transforms/GlobalOpt/shrink-global-to-bool.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/GlobalOpt/shrink-global-to-bool.ll
@@ -0,0 +1,41 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -passes=globalopt --mtriple=x86_64-unknown-linux-gnu < %s -S | FileCheck %s
+; RUN: opt -passes=globalopt --mtriple=amdgcn-amd-amdhsa < %s -S | FileCheck %s
+; REQUIRES: amdgpu-registered-target
+
+ at gvar = internal unnamed_addr global i32 undef
+ at lvar = internal unnamed_addr addrspace(3) global i32 undef
+
+; Should optimize @gvar.
+; CHECK-NOT: @gvar
+
+; Negative test for AS(3). Skip shrink global to bool optimization.
+; CHECK: @lvar = internal unnamed_addr addrspace(3) global i32 undef
+
+define void @test_global_var() {
+; CHECK-LABEL: @test_global_var(
+; CHECK: store volatile i32 10, i32* undef, align 4
+;
+entry:
+ store i32 10, i32* @gvar
+ br label %exit
+exit:
+ %ld = load i32, i32* @gvar
+ store volatile i32 %ld, i32* undef
+ ret void
+}
+
+define void @test_lds_var() {
+; CHECK-LABEL: @test_lds_var(
+; CHECK: store i32 10, i32 addrspace(3)* @lvar, align 4
+; CHECK: [[LD:%.*]] = load i32, i32 addrspace(3)* @lvar, align 4
+; CHECK: store volatile i32 [[LD]], i32* undef, align 4
+;
+entry:
+ store i32 10, i32 addrspace(3)* @lvar
+ br label %exit
+exit:
+ %ld = load i32, i32 addrspace(3)* @lvar
+ store volatile i32 %ld, i32* undef
+ ret void
+}
Index: llvm/lib/Transforms/IPO/GlobalOpt.cpp
===================================================================
--- llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -1612,6 +1612,9 @@
Function &StoreFn =
const_cast<Function &>(*GS.StoredOnceStore->getFunction());
+ bool CanHaveNonUndefGlobalInitializer =
+ GetTTI(StoreFn).canHaveNonUndefGlobalInitializerInAddressSpace(
+ GV->getType()->getAddressSpace());
// If the initial value for the global was an undef value, and if only
// one other value was stored into it, we can just change the
// initializer to be the stored value, then delete all stores to the
@@ -1621,8 +1624,7 @@
// shared memory (AS 3).
if (SOVConstant && SOVConstant->getType() == GV->getValueType() &&
isa<UndefValue>(GV->getInitializer()) &&
- GetTTI(StoreFn).canHaveNonUndefGlobalInitializerInAddressSpace(
- GV->getType()->getAddressSpace())) {
+ CanHaveNonUndefGlobalInitializer) {
// Change the initial value here.
GV->setInitializer(SOVConstant);
@@ -1645,8 +1647,10 @@
return true;
// Otherwise, if the global was not a boolean, we can shrink it to be a
- // boolean.
- if (SOVConstant && GS.Ordering == AtomicOrdering::NotAtomic) {
+ // boolean. Skip this optimization for AS that doesn't allow an initializer.
+ if (SOVConstant && GS.Ordering == AtomicOrdering::NotAtomic &&
+ (!isa<UndefValue>(GV->getInitializer()) ||
+ CanHaveNonUndefGlobalInitializer)) {
if (TryToShrinkGlobalToBoolean(GV, SOVConstant)) {
++NumShrunkToBool;
return true;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109823.372751.patch
Type: text/x-patch
Size: 3190 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210915/b1002902/attachment.bin>
More information about the llvm-commits
mailing list