Skip to content

Commit 0ff0218

Browse files
committed
Stash
1 parent a3709f9 commit 0ff0218

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4044,6 +4044,37 @@ def CIR_ExpectOp : CIR_Op<"expect", [
40444044
}];
40454045
}
40464046

4047+
//===----------------------------------------------------------------------===//
4048+
// PtrDiffOp
4049+
//===----------------------------------------------------------------------===//
4050+
4051+
def CIR_PtrDiffOp : CIR_Op<"ptr_diff", [Pure, SameTypeOperands]> {
4052+
let summary = "Pointer subtraction arithmetic";
4053+
let description = [{
4054+
The cir.ptr_diff operation computes the difference between two pointers that
4055+
have the same element type
4056+
4057+
The result reflects the ABI-defined size of the pointed-to type. For example,
4058+
subtracting two !cir.ptr<!u64i> values may yield 1, representing an 8-byte
4059+
difference. In contrast, for pointers to void or function types, a result of
4060+
8 corresponds to an 8-byte difference.
4061+
4062+
Example:
4063+
4064+
```mlir
4065+
%7 = cir.ptr_diff %0, %1 : !cir.ptr<!u64i> -> !u64i
4066+
```
4067+
}];
4068+
4069+
let arguments = (ins CIR_PointerType:$lhs, CIR_PointerType:$rhs);
4070+
let results = (outs CIR_AnyFundamentalIntType:$result);
4071+
4072+
let assemblyFormat = [{
4073+
$lhs `,` $rhs `:` qualified(type($lhs)) `->` qualified(type($result))
4074+
attr-dict
4075+
}];
4076+
}
4077+
40474078
//===----------------------------------------------------------------------===//
40484079
// Floating Point Ops
40494080
//===----------------------------------------------------------------------===//

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ struct MissingFeatures {
321321
static bool ifOp() { return false; }
322322
static bool invokeOp() { return false; }
323323
static bool labelOp() { return false; }
324-
static bool ptrDiffOp() { return true; }
324+
static bool ptrDiffOp() { return false; }
325+
static bool llvmLoweringPtrDiffConsidersPointee() {return false; }
325326
static bool ptrStrideOp() { return false; }
326327
static bool switchOp() { return false; }
327328
static bool throwOp() { return false; }

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1734,7 +1734,7 @@ mlir::Value ScalarExprEmitter::emitSub(const BinOpInfo &ops) {
17341734
// LLVM we shall take VLA's, division by element size, etc.
17351735
//
17361736
// See more in `EmitSub` in CGExprScalar.cpp.
1737-
assert(!cir::MissingFeatures::ptrDiffOp());
1737+
assert(!cir::MissingFeatures::llvmLoweringPtrDiffConsidersPointee());
17381738
return builder.create<cir::PtrDiffOp>(cgf.getLoc(ops.loc), cgf.PtrDiffTy,
17391739
ops.lhs, ops.rhs);
17401740
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,15 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
14991499
return mlir::success();
15001500
}
15011501

1502+
uint64_t CIRToLLVMPtrDiffOpLowering::getTypeSize(mlir::Type type,
1503+
mlir::Operation &op) const {
1504+
mlir::DataLayout layout(op.getParentOfType<mlir::ModuleOp>());
1505+
// For LLVM purposes we treat void as u8.
1506+
if (isa<cir::VoidType>(type))
1507+
type = cir::IntType::get(type.getContext(), 8, /*isSigned=*/false);
1508+
return llvm::divideCeil(layout.getTypeSizeInBits(type), 8);
1509+
}
1510+
15021511
mlir::LogicalResult CIRToLLVMPtrDiffOpLowering::matchAndRewrite(
15031512
cir::PtrDiffOp op, OpAdaptor adaptor,
15041513
mlir::ConversionPatternRewriter &rewriter) const {

0 commit comments

Comments
 (0)