[llvm] b2fcab1 - GlobalOpt: Add some test coverage for addrspacecast treatment

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 29 13:06:07 PDT 2023


Author: Matt Arsenault
Date: 2023-04-29T16:06:01-04:00
New Revision: b2fcab1c3450f1ce035913a0510a54a033a7de44

URL: https://github.com/llvm/llvm-project/commit/b2fcab1c3450f1ce035913a0510a54a033a7de44
DIFF: https://github.com/llvm/llvm-project/commit/b2fcab1c3450f1ce035913a0510a54a033a7de44.diff

LOG: GlobalOpt: Add some test coverage for addrspacecast treatment

Added: 
    llvm/test/Transforms/GlobalOpt/addrspacecast.ll

Modified: 
    

Removed: 
    


################################################################################
diff  --git a/llvm/test/Transforms/GlobalOpt/addrspacecast.ll b/llvm/test/Transforms/GlobalOpt/addrspacecast.ll
new file mode 100644
index 0000000000000..7920629664d54
--- /dev/null
+++ b/llvm/test/Transforms/GlobalOpt/addrspacecast.ll
@@ -0,0 +1,106 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
+; RUN: opt -S -passes=globalopt < %s | FileCheck %s
+
+; Test behavior of trying to fold access of global variables
+; initialized one time through addrspacecast.
+
+ at as0_ptr = internal global ptr null
+ at as1_ptr = internal global ptr addrspace(1) null
+ at as2_ptr = internal global ptr addrspace(2) null
+ at as1_null_valid_ptr = internal global ptr addrspace(1) null
+ at as0_null_valid_ptr = internal global ptr null
+
+ at Actual.as0 = internal global i32 1234
+ at Actual.as1 = internal addrspace(1) global i32 5678
+ at Actual.as2 = internal addrspace(2) global i32 91011
+ at Actual.as1.null.valid = internal addrspace(1) global i32 91011
+ at Actual.as0.null.valid = internal global i32 9999
+
+; Initialize each global once.
+define void @init() {
+; CHECK-LABEL: define void @init() local_unnamed_addr {
+; CHECK-NEXT:    store ptr addrspace(1) @Actual.as1, ptr @as1_ptr, align 8
+; CHECK-NEXT:    store ptr addrspace(2) @Actual.as2, ptr @as2_ptr, align 8
+; CHECK-NEXT:    store ptr addrspace(1) @Actual.as1.null.valid, ptr @as1_null_valid_ptr, align 8
+; CHECK-NEXT:    store ptr @Actual.as0.null.valid, ptr @as0_null_valid_ptr, align 8
+; CHECK-NEXT:    ret void
+;
+  store ptr @Actual.as0, ptr @as0_ptr
+  store ptr addrspace(1) @Actual.as1, ptr @as1_ptr
+  store ptr addrspace(2) @Actual.as2, ptr @as2_ptr
+  store ptr addrspace(1) @Actual.as1.null.valid, ptr @as1_null_valid_ptr
+  store ptr @Actual.as0.null.valid, ptr @as0_null_valid_ptr
+  ret void
+}
+
+; Access stored value is null in the default address space, which we
+; can assume cannot alias a valid object in another address space.
+define i32 @load_from_as0_ptr_in_as1() {
+; CHECK-LABEL: define i32 @load_from_as0_ptr_in_as1() local_unnamed_addr {
+; CHECK-NEXT:    ret i32 1234
+;
+  %ptr = load ptr, ptr @as0_ptr
+  %cast = addrspacecast ptr %ptr to ptr addrspace(1)
+  %load = load i32, ptr addrspace(1) %cast
+  ret i32 %load
+}
+
+; Cast is to default address space and access is done through default
+; address space in a function where null is valid.
+define i32 @load_from_as0_ptr_in_as1_null_is_valid() null_pointer_is_valid {
+; CHECK-LABEL: define i32 @load_from_as0_ptr_in_as1_null_is_valid
+; CHECK-SAME: () local_unnamed_addr #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:    [[PTR:%.*]] = load ptr addrspace(1), ptr @as1_null_valid_ptr, align 8
+; CHECK-NEXT:    [[CAST:%.*]] = addrspacecast ptr addrspace(1) [[PTR]] to ptr
+; CHECK-NEXT:    [[LOAD:%.*]] = load i32, ptr [[CAST]], align 4
+; CHECK-NEXT:    ret i32 [[LOAD]]
+;
+  %ptr = load ptr addrspace(1), ptr @as1_null_valid_ptr
+  %cast = addrspacecast ptr addrspace(1) %ptr to ptr
+  %load = load i32, ptr %cast
+  ret i32 %load
+}
+
+; Original addrspace(0) null was valid, and we also have to assume the
+; access is valid in addrspace(1)
+define i32 @load_from_as1_ptr_in_as0_null_is_valid() null_pointer_is_valid {
+; CHECK-LABEL: define i32 @load_from_as1_ptr_in_as0_null_is_valid
+; CHECK-SAME: () local_unnamed_addr #[[ATTR0]] {
+; CHECK-NEXT:    [[PTR:%.*]] = load ptr, ptr @as0_null_valid_ptr, align 8
+; CHECK-NEXT:    [[CAST:%.*]] = addrspacecast ptr [[PTR]] to ptr addrspace(1)
+; CHECK-NEXT:    [[LOAD:%.*]] = load i32, ptr addrspace(1) [[CAST]], align 4
+; CHECK-NEXT:    ret i32 [[LOAD]]
+;
+  %ptr = load ptr, ptr @as0_null_valid_ptr
+  %cast = addrspacecast ptr %ptr to ptr addrspace(1)
+  %load = load i32, ptr addrspace(1) %cast
+  ret i32 %load
+}
+
+; Underlying pointer could be valid, can't fold this out.
+define i32 @load_from_as1_ptr_in_as0() {
+; CHECK-LABEL: define i32 @load_from_as1_ptr_in_as0() local_unnamed_addr {
+; CHECK-NEXT:    [[PTR:%.*]] = load ptr addrspace(1), ptr @as1_ptr, align 8
+; CHECK-NEXT:    [[CAST:%.*]] = addrspacecast ptr addrspace(1) [[PTR]] to ptr
+; CHECK-NEXT:    [[LOAD:%.*]] = load i32, ptr [[CAST]], align 4
+; CHECK-NEXT:    ret i32 [[LOAD]]
+;
+  %ptr = load ptr addrspace(1), ptr @as1_ptr
+  %cast = addrspacecast ptr addrspace(1) %ptr to ptr
+  %load = load i32, ptr %cast
+  ret i32 %load
+}
+
+; Two non-default address spaces, can't fold.
+define i32 @load_from_as2_ptr_in_as1() {
+; CHECK-LABEL: define i32 @load_from_as2_ptr_in_as1() local_unnamed_addr {
+; CHECK-NEXT:    [[PTR:%.*]] = load ptr addrspace(2), ptr @as2_ptr, align 8
+; CHECK-NEXT:    [[CAST:%.*]] = addrspacecast ptr addrspace(2) [[PTR]] to ptr addrspace(1)
+; CHECK-NEXT:    [[LOAD:%.*]] = load i32, ptr addrspace(1) [[CAST]], align 4
+; CHECK-NEXT:    ret i32 [[LOAD]]
+;
+  %ptr = load ptr addrspace(2), ptr @as2_ptr
+  %cast = addrspacecast ptr addrspace(2) %ptr to ptr addrspace(1)
+  %load = load i32, ptr addrspace(1) %cast
+  ret i32 %load
+}


        


More information about the llvm-commits mailing list