Skip to content

Commit 2da5665

Browse files
PragmaTwicelanza
authored andcommitted
[CIR][CIRGen] Support for dereferencing void pointers (#595)
In this PR, we support for dereferencing void pointers as a GNU C extension. This include two modification: - In CIRGen, we support to build ReturnStmt with void return type. - In LowerToLLVM, we support to lower CIR load with void result type to LLVM. It's a part of #579, since I would like to split it to two tasks: - support pointer arithmetic for function types (#594) - **support to dereference void pointer (this PR)**
1 parent 9adc2c7 commit 2da5665

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -2557,9 +2557,18 @@ mlir::Value CIRGenFunction::buildLoadOfScalar(Address Addr, bool Volatile,
25572557
llvm_unreachable("NYI: Special treatment of 3-element vector load");
25582558
}
25592559

2560-
mlir::cir::LoadOp Load = builder.create<mlir::cir::LoadOp>(
2561-
Loc, Addr.getElementType(), Addr.getPointer(), /* deref */ false,
2562-
Volatile, ::mlir::cir::MemOrderAttr{});
2560+
auto Ptr = Addr.getPointer();
2561+
auto ElemTy = Addr.getElementType();
2562+
if (ElemTy.isa<mlir::cir::VoidType>()) {
2563+
ElemTy = mlir::cir::IntType::get(builder.getContext(), 8, true);
2564+
auto ElemPtrTy = mlir::cir::PointerType::get(builder.getContext(), ElemTy);
2565+
Ptr = builder.create<mlir::cir::CastOp>(Loc, ElemPtrTy,
2566+
mlir::cir::CastKind::bitcast, Ptr);
2567+
}
2568+
2569+
mlir::cir::LoadOp Load =
2570+
builder.create<mlir::cir::LoadOp>(Loc, ElemTy, Ptr, /* deref */ false,
2571+
Volatile, ::mlir::cir::MemOrderAttr{});
25632572

25642573
if (isNontemporal) {
25652574
llvm_unreachable("NYI");

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+
buildAnyExpr(RV);
491491
}
492492
} else if (!RV) {
493493
// Do nothing (return value is left uninitialized)

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

+27-3
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,33 @@ FP f7(FP a, int b) { return a - b; }
8282
// Similar to f7, just make sure it does not crash.
8383
FP f7_1(FP a, int b) { return (a -= b); }
8484

85-
// FIXME: add support for the remaining ones.
86-
// void f8(void *a, int b) { return *(a + b); }
87-
// void f8_1(void *a, int b) { return a[b]; }
85+
void f8(void *a, int b) { return *(a + b); }
86+
// CIR-LABEL: f8
87+
// CIR: %[[PTR:.*]] = cir.load {{.*}} : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void>
88+
// CIR: %[[STRIDE:.*]] = cir.load {{.*}} : !cir.ptr<!s32i>, !s32i
89+
// CIR: cir.ptr_stride(%[[PTR]] : !cir.ptr<!void>, %[[STRIDE]] : !s32i)
90+
// CIR: cir.return
91+
92+
// LLVM-LABEL: f8
93+
// LLVM: %[[PTR:.*]] = load ptr, ptr {{.*}}, align 8
94+
// LLVM: %[[TOEXT:.*]] = load i32, ptr {{.*}}, align 4
95+
// LLVM: %[[STRIDE:.*]] = sext i32 %[[TOEXT]] to i64
96+
// LLVM: getelementptr i8, ptr %[[PTR]], i64 %[[STRIDE]]
97+
// LLVM: ret void
98+
99+
void f8_1(void *a, int b) { return a[b]; }
100+
// CIR-LABEL: f8_1
101+
// CIR: %[[PTR:.*]] = cir.load {{.*}} : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void>
102+
// CIR: %[[STRIDE:.*]] = cir.load {{.*}} : !cir.ptr<!s32i>, !s32i
103+
// CIR: cir.ptr_stride(%[[PTR]] : !cir.ptr<!void>, %[[STRIDE]] : !s32i)
104+
// CIR: cir.return
105+
106+
// LLVM-LABEL: f8_1
107+
// LLVM: %[[PTR:.*]] = load ptr, ptr {{.*}}, align 8
108+
// LLVM: %[[TOEXT:.*]] = load i32, ptr {{.*}}, align 4
109+
// LLVM: %[[STRIDE:.*]] = sext i32 %[[TOEXT]] to i64
110+
// LLVM: getelementptr i8, ptr %[[PTR]], i64 %[[STRIDE]]
111+
// LLVM: ret void
88112

89113
unsigned char *p(unsigned int x) {
90114
unsigned char *p;

0 commit comments

Comments
 (0)