Skip to content

Commit fa8dad0

Browse files
committed
[CIR][CIRGen&LowerToLLVM] Support for dereferencing void pointers
1 parent 3e4d5e6 commit fa8dad0

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

clang/lib/CIR/CodeGen/CIRGenStmt.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ mlir::LogicalResult CIRGenFunction::buildReturnStmt(const ReturnStmt &S) {
487487
// Make sure not to return anything, but evaluate the expression
488488
// for side effects.
489489
if (RV) {
490-
assert(0 && "not implemented");
490+
buildScalarExpr(RV);
491491
}
492492
} else if (!RV) {
493493
// Do nothing (return value is left uninitialized)

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

+7
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,13 @@ class CIRLoadLowering : public mlir::OpConversionPattern<mlir::cir::LoadOp> {
864864
mlir::LogicalResult
865865
matchAndRewrite(mlir::cir::LoadOp op, OpAdaptor adaptor,
866866
mlir::ConversionPatternRewriter &rewriter) const override {
867+
// if the result type is void, we can just remove the load operation since
868+
// it's useless
869+
if (op.getResult().getType().isa<mlir::cir::VoidType>()) {
870+
rewriter.eraseOp(op);
871+
return mlir::LogicalResult::success();
872+
}
873+
867874
const auto llvmTy =
868875
getTypeConverter()->convertType(op.getResult().getType());
869876
unsigned alignment = 0;

clang/test/CIR/CodeGen/pointer-arith-ext.c

+28-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,34 @@ void *f4_1(void *a, int b) { return (a -= b); }
5555
// FP f6_1(int a, FP b) { return (a += b); }
5656
// FP f7(FP a, int b) { return a - b; }
5757
// FP f7_1(FP a, int b) { return (a -= b); }
58-
// void f8(void *a, int b) { return *(a + b); }
59-
// void f8_1(void *a, int b) { return a[b]; }
58+
59+
void f8(void *a, int b) { return *(a + b); }
60+
// CIR-LABEL: f8
61+
// CIR: %[[PTR:.*]] = cir.load {{.*}} : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void>
62+
// CIR: %[[STRIDE:.*]] = cir.load {{.*}} : !cir.ptr<!s32i>, !s32i
63+
// CIR: cir.ptr_stride(%[[PTR]] : !cir.ptr<!void>, %[[STRIDE]] : !s32i)
64+
// CIR: cir.return
65+
66+
// LLVM-LABEL: f8
67+
// LLVM: %[[PTR:.*]] = load ptr, ptr {{.*}}, align 8
68+
// LLVM: %[[TOEXT:.*]] = load i32, ptr {{.*}}, align 4
69+
// LLVM: %[[STRIDE:.*]] = sext i32 %[[TOEXT]] to i64
70+
// LLVM: getelementptr i8, ptr %[[PTR]], i64 %[[STRIDE]]
71+
// LLVM: ret void
72+
73+
void f8_1(void *a, int b) { return a[b]; }
74+
// CIR-LABEL: f8_1
75+
// CIR: %[[PTR:.*]] = cir.load {{.*}} : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void>
76+
// CIR: %[[STRIDE:.*]] = cir.load {{.*}} : !cir.ptr<!s32i>, !s32i
77+
// CIR: cir.ptr_stride(%[[PTR]] : !cir.ptr<!void>, %[[STRIDE]] : !s32i)
78+
// CIR: cir.return
79+
80+
// LLVM-LABEL: f8_1
81+
// LLVM: %[[PTR:.*]] = load ptr, ptr {{.*}}, align 8
82+
// LLVM: %[[TOEXT:.*]] = load i32, ptr {{.*}}, align 4
83+
// LLVM: %[[STRIDE:.*]] = sext i32 %[[TOEXT]] to i64
84+
// LLVM: getelementptr i8, ptr %[[PTR]], i64 %[[STRIDE]]
85+
// LLVM: ret void
6086

6187
unsigned char *p(unsigned int x) {
6288
unsigned char *p;

0 commit comments

Comments
 (0)