Skip to content

Commit 095c2ae

Browse files
yugrbcardosolopes
authored andcommitted
[CIR][Lowering] Support lowering of cir.const with GlobalViewAttr (gh-352) (#363)
The error manifested in code like ``` int a[16]; int *const p = a; void foo() { p[0]; } ``` It's one the most frequent errors in current llvm-test-suite. I've added the test to globals.cir which is currently XFAILed, I think @gitoleg will fix it soon. Co-authored-by: Bruno Cardoso Lopes <[email protected]>
1 parent 8b01168 commit 095c2ae

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,12 @@ class CIRConstantLowering
10561056
return mlir::success();
10571057
}
10581058
}
1059+
// Lower GlobalViewAttr to llvm.mlir.addressof
1060+
if (auto gv = op.getValue().dyn_cast<mlir::cir::GlobalViewAttr>()) {
1061+
auto newOp = lowerCirAttrAsValue(op, gv, rewriter, getTypeConverter());
1062+
rewriter.replaceOp(op, newOp);
1063+
return mlir::success();
1064+
}
10591065
attr = op.getValue();
10601066
}
10611067
// TODO(cir): constant arrays are currently just pushed into the stack using

clang/test/CIR/Lowering/globals.cir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,15 @@ module {
177177
//MLIR: %[[RES8:.*]] = llvm.getelementptr %[[RES7]][0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"struct.anon.1", (ptr)>
178178
//MLIR: %[[RES9:.*]] = llvm.load %[[RES8]] : !llvm.ptr -> !llvm.ptr
179179
//MLIR: llvm.call %[[RES9]]({{.*}}) : !llvm.ptr, (i32) -> ()
180+
181+
cir.global external @zero_array = #cir.zero : !cir.array<!s32i x 16>
182+
cir.func @use_zero_array() {
183+
%0 = cir.const(#cir.global_view<@zero_array> : !cir.ptr<!s32i>) : !cir.ptr<!s32i>
184+
%1 = cir.const(#cir.int<0> : !s32i) : !s32i
185+
%2 = cir.ptr_stride(%0 : !cir.ptr<!s32i>, %1 : !s32i), !cir.ptr<!s32i>
186+
%3 = cir.load %2 : cir.ptr <!s32i>, !s32i
187+
cir.return
188+
}
189+
// MLIR: %0 = llvm.mlir.addressof @zero_array
190+
180191
}

0 commit comments

Comments
 (0)