Skip to content

Commit

Permalink
[CIR][Lowering] Support lowering of cir.const with GlobalViewAttr (gh…
Browse files Browse the repository at this point in the history
…-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]>
  • Loading branch information
2 people authored and lanza committed Apr 29, 2024
1 parent ceffdf4 commit 983f5bb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,12 @@ class CIRConstantLowering
return mlir::success();
}
}
// Lower GlobalViewAttr to llvm.mlir.addressof
if (auto gv = op.getValue().dyn_cast<mlir::cir::GlobalViewAttr>()) {
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
Expand Down
11 changes: 11 additions & 0 deletions clang/test/CIR/Lowering/globals.cir
Original file line number Diff line number Diff line change
Expand Up @@ -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<!s32i x 16>
cir.func @use_zero_array() {
%0 = cir.const(#cir.global_view<@zero_array> : !cir.ptr<!s32i>) : !cir.ptr<!s32i>
%1 = cir.const(#cir.int<0> : !s32i) : !s32i
%2 = cir.ptr_stride(%0 : !cir.ptr<!s32i>, %1 : !s32i), !cir.ptr<!s32i>
%3 = cir.load %2 : cir.ptr <!s32i>, !s32i
cir.return
}
// MLIR: %0 = llvm.mlir.addressof @zero_array

}

0 comments on commit 983f5bb

Please sign in to comment.