From c2e8f2af6a34820796170a98200c5d43a521980b Mon Sep 17 00:00:00 2001 From: Yury Gribov Date: Thu, 4 Jan 2024 23:37:10 +0300 Subject: [PATCH] [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 --- clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 6 ++++++ clang/test/CIR/Lowering/globals.cir | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index bc1edff65af6..786cca9425a6 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -1056,6 +1056,12 @@ class CIRConstantLowering return mlir::success(); } } + // Lower GlobalViewAttr to llvm.mlir.addressof + if (auto gv = op.getValue().dyn_cast()) { + auto newOp = lowerCirAttrAsValue(op, gv, rewriter, getTypeConverter()); + rewriter.replaceOp(op, newOp); + return mlir::success(); + } attr = op.getValue(); } // TODO(cir): constant arrays are currently just pushed into the stack using diff --git a/clang/test/CIR/Lowering/globals.cir b/clang/test/CIR/Lowering/globals.cir index 699bf3fd3b5a..99bfa76dd3a8 100644 --- a/clang/test/CIR/Lowering/globals.cir +++ b/clang/test/CIR/Lowering/globals.cir @@ -177,4 +177,15 @@ module { //MLIR: %[[RES8:.*]] = llvm.getelementptr %[[RES7]][0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"struct.anon.1", (ptr)> //MLIR: %[[RES9:.*]] = llvm.load %[[RES8]] : !llvm.ptr -> !llvm.ptr //MLIR: llvm.call %[[RES9]]({{.*}}) : !llvm.ptr, (i32) -> () + + cir.global external @zero_array = #cir.zero : !cir.array + cir.func @use_zero_array() { + %0 = cir.const(#cir.global_view<@zero_array> : !cir.ptr) : !cir.ptr + %1 = cir.const(#cir.int<0> : !s32i) : !s32i + %2 = cir.ptr_stride(%0 : !cir.ptr, %1 : !s32i), !cir.ptr + %3 = cir.load %2 : cir.ptr , !s32i + cir.return + } + // MLIR: %0 = llvm.mlir.addressof @zero_array + }