Skip to content

Commit 5580f73

Browse files
authored
[CIR] Clean up cir.objsize to follow new format and add documentation (#1931)
1 parent 823e943 commit 5580f73

File tree

6 files changed

+47
-36
lines changed

6 files changed

+47
-36
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -331,29 +331,39 @@ def CIR_DynamicCastOp : CIR_Op<"dyn_cast"> {
331331
// ObjSizeOp
332332
//===----------------------------------------------------------------------===//
333333

334-
def CIR_SizeInfoType : CIR_I32EnumAttr< "SizeInfoType", "size info type", [
335-
I32EnumAttrCase<"Min", 0, "min">,
336-
I32EnumAttrCase<"Max", 1, "max">
337-
]>;
338-
339334
def CIR_ObjSizeOp : CIR_Op<"objsize", [Pure]> {
340-
let summary = "Conversion between values of different types";
335+
let summary = "Implements llvm.objsize builtin.";
336+
let description = [{
337+
The `cir.objsize` operation models the behavior of the `llvm.objectsize`
338+
builtins in Clang. It returns the number of accessible bytes past ptr.
339+
340+
The `kind` argument determines whether `cir.objsize` returns 0 (if
341+
true) or -1 (if false) when the object size is unknown. Corresponds to
342+
`llvm.objectsize`'s `min` argument.
343+
344+
The `dynamic` attribute determines if the value should be evaluated at
345+
runtime. Corresponds to `llvm.objectsize`'s `dynamic` argument.
346+
347+
Example:
348+
349+
```mlir
350+
%size = cir.objsize min %ptr : !cir.ptr<i32> -> i64
351+
%dsize = cir.objsize max dynamic %ptr : !cir.ptr<i32> -> i64
352+
```
353+
}];
341354

342355
let arguments = (ins
343356
CIR_PointerType:$ptr,
344-
CIR_SizeInfoType:$kind,
357+
UnitAttr:$min,
345358
UnitAttr:$dynamic
346359
);
347360

348361
let results = (outs CIR_AnyFundamentalIntType:$result);
349362

350363
let assemblyFormat = [{
351-
`(`
352-
$ptr `:` type($ptr) `,`
353-
$kind
354-
(`,` `dynamic` $dynamic^)?
355-
`)`
356-
`->` type($result) attr-dict
364+
(`min` $min^) : (`max`)?
365+
(`dynamic` $dynamic^)?
366+
$ptr `:` qualified(type($ptr)) `->` qualified(type($result)) attr-dict
357367
}];
358368
}
359369

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3010,12 +3010,11 @@ mlir::Value CIRGenFunction::emitBuiltinObjectSize(const Expr *E, unsigned Type,
30103010

30113011
// LLVM intrinsics (which CIR lowers to at some point, only supports 0
30123012
// and 2, account for that right now.
3013-
cir::SizeInfoType sizeInfoTy =
3014-
((Type & 2) != 0) ? cir::SizeInfoType::Min : cir::SizeInfoType::Max;
3013+
bool sizeInfoTy = ((Type & 2) != 0);
30153014
// TODO(cir): Heads up for LLVM lowering, For GCC compatibility,
30163015
// __builtin_object_size treat NULL as unknown size.
3017-
return builder.create<cir::ObjSizeOp>(getLoc(E->getSourceRange()), ResType,
3018-
Ptr, sizeInfoTy, IsDynamic);
3016+
return cir::ObjSizeOp::create(builder, getLoc(E->getSourceRange()), ResType,
3017+
Ptr, sizeInfoTy, IsDynamic);
30193018
}
30203019

30213020
mlir::Value CIRGenFunction::evaluateOrEmitBuiltinObjectSize(

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3375,18 +3375,20 @@ mlir::LogicalResult CIRToLLVMObjSizeOpLowering::matchAndRewrite(
33753375
auto llvmResTy = getTypeConverter()->convertType(op.getType());
33763376
auto loc = op->getLoc();
33773377

3378-
cir::SizeInfoType kindInfo = op.getKind();
3379-
auto falseValue =
3380-
rewriter.create<mlir::LLVM::ConstantOp>(loc, rewriter.getI1Type(), false);
3381-
auto trueValue =
3382-
rewriter.create<mlir::LLVM::ConstantOp>(loc, rewriter.getI1Type(), true);
3383-
3384-
replaceOpWithCallLLVMIntrinsicOp(
3385-
rewriter, op, "llvm.objectsize", llvmResTy,
3386-
mlir::ValueRange{adaptor.getPtr(),
3387-
kindInfo == cir::SizeInfoType::Max ? falseValue
3388-
: trueValue,
3389-
trueValue, op.getDynamic() ? trueValue : falseValue});
3378+
auto i1Ty = rewriter.getI1Type();
3379+
3380+
auto i1Val = [&rewriter, &loc, &i1Ty](bool val) {
3381+
return mlir::LLVM::ConstantOp::create(rewriter, loc, i1Ty, val);
3382+
};
3383+
3384+
// clang-format off
3385+
replaceOpWithCallLLVMIntrinsicOp(rewriter, op, "llvm.objectsize", llvmResTy, {
3386+
/*ptr=*/adaptor.getPtr(),
3387+
/*min=*/i1Val(op.getMin()),
3388+
/*nullunknown=*/i1Val(true),
3389+
/*dynamic=*/i1Val(op.getDynamic())
3390+
});
3391+
// clang-format on
33903392

33913393
return mlir::LogicalResult::success();
33923394
}

clang/test/CIR/CodeGen/libcall.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void t(const char* fmt, ...) {
4343
// CHECK: %0 = cir.alloca !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>>, ["m", init] {alignment = 8 : i64}
4444

4545
// CHECK: %3 = cir.load{{.*}} %0 : !cir.ptr<!cir.ptr<!s8i>>, !cir.ptr<!s8i>
46-
// CHECK: %4 = cir.objsize(%3 : <!s8i>, max) -> !u64i
46+
// CHECK: %4 = cir.objsize max %3 : !cir.ptr<!s8i> -> !u64i
4747
// CHECK: %5 = cir.call @_ZL6strlenPKcU17pass_object_size0(%3, %4) : (!cir.ptr<!s8i>, !u64i) -> !u64i
4848

4949
// CHECK: cir.func private @__vsnprintf_chk

clang/test/CIR/CodeGen/pass-object-size.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ void c() {
1515
// CIR: cir.func no_proto dso_local @c()
1616
// CIR: [[TMP0:%.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, %{{[0-9]+}} : !u64i, ["vla"] {alignment = 16 : i64}
1717
// CIR: [[TMP1:%.*]] = cir.cast bitcast [[TMP0]] : !cir.ptr<!s32i> -> !cir.ptr<!void>
18-
// CIR-NEXT: [[TMP2:%.*]] = cir.objsize([[TMP1]] : <!void>, max) -> !u64i
18+
// CIR-NEXT: [[TMP2:%.*]] = cir.objsize max [[TMP1]] : !cir.ptr<!void> -> !u64i
1919
// CIR-NEXT: cir.call @b([[TMP1]], [[TMP2]]) : (!cir.ptr<!void>, !u64i) -> ()
2020
// CIR: [[TMP3:%.*]] = cir.cast bitcast [[TMP0]] : !cir.ptr<!s32i> -> !cir.ptr<!void>
21-
// CIR: [[TMP4:%.*]] = cir.objsize([[TMP3]] : <!void>, min) -> !u64i
21+
// CIR: [[TMP4:%.*]] = cir.objsize min [[TMP3]] : !cir.ptr<!void> -> !u64i
2222
// CIR-NEXT: cir.call @e([[TMP3]], [[TMP4]]) : (!cir.ptr<!void>, !u64i) -> ()
2323

2424
// LLVM: define dso_local void @c()

clang/test/CIR/IR/cir-ops.cir

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ module {
5353
cir.func @os() {
5454
%0 = cir.alloca !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>>, ["m", init] {alignment = 8 : i64}
5555
%3 = cir.load %0 : !cir.ptr<!cir.ptr<!s8i>>, !cir.ptr<!s8i>
56-
%4 = cir.objsize(%3 : <!s8i>, max) -> !u64i
57-
%5 = cir.objsize(%3 : <!s8i>, min) -> !u64i
56+
%4 = cir.objsize max %3 : !cir.ptr<!s8i> -> !u64i
57+
%5 = cir.objsize min %3 : !cir.ptr<!s8i> -> !u64i
5858
cir.return
5959
}
6060

@@ -105,8 +105,8 @@ module {
105105
// CHECK: cir.func @os() {
106106
// CHECK-NEXT: %0 = cir.alloca !cir.ptr<!s8i>, !cir.ptr<!cir.ptr<!s8i>>, ["m", init] {alignment = 8 : i64}
107107
// CHECK-NEXT: %1 = cir.load %0 : !cir.ptr<!cir.ptr<!s8i>>, !cir.ptr<!s8i>
108-
// CHECK-NEXT: %2 = cir.objsize(%1 : <!s8i>, max) -> !u64i
109-
// CHECK-NEXT: %3 = cir.objsize(%1 : <!s8i>, min) -> !u64i
108+
// CHECK-NEXT: %2 = cir.objsize max %1 : !cir.ptr<!s8i> -> !u64i
109+
// CHECK-NEXT: %3 = cir.objsize min %1 : !cir.ptr<!s8i> -> !u64i
110110
// CHECK-NEXT: cir.return
111111
// CHECK-NEXT: }
112112

0 commit comments

Comments
 (0)