Skip to content

Commit db85047

Browse files
committed
Stash
1 parent 9f2ec3c commit db85047

File tree

5 files changed

+75
-15
lines changed

5 files changed

+75
-15
lines changed

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ 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 false; }
324+
static bool ptrDiffOp() { return true; }
325325
static bool ptrStrideOp() { return false; }
326326
static bool switchOp() { return false; }
327327
static bool throwOp() { return false; }

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,8 +1735,7 @@ mlir::Value ScalarExprEmitter::emitSub(const BinOpInfo &ops) {
17351735
//
17361736
// See more in `EmitSub` in CGExprScalar.cpp.
17371737
assert(!cir::MissingFeatures::ptrDiffOp());
1738-
cgf.cgm.errorNYI("ptrdiff");
1739-
return {};
1738+
return builder.create<cir::PtrDiffOp>(cgf.getLoc(ops.loc), cgf.PtrDiffTy, ops.lhs, ops.rhs);
17401739
}
17411740

17421741
mlir::Value ScalarExprEmitter::emitShl(const BinOpInfo &ops) {

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

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

1502+
mlir::LogicalResult CIRToLLVMPtrDiffOpLowering::matchAndRewrite(
1503+
cir::PtrDiffOp op, OpAdaptor adaptor,
1504+
mlir::ConversionPatternRewriter &rewriter) const {
1505+
auto dstTy = mlir::cast<cir::IntType>(op.getType());
1506+
auto llvmDstTy = getTypeConverter()->convertType(dstTy);
1507+
1508+
auto lhs = rewriter.create<mlir::LLVM::PtrToIntOp>(op.getLoc(), llvmDstTy,
1509+
adaptor.getLhs());
1510+
auto rhs = rewriter.create<mlir::LLVM::PtrToIntOp>(op.getLoc(), llvmDstTy,
1511+
adaptor.getRhs());
1512+
1513+
auto diff =
1514+
rewriter.create<mlir::LLVM::SubOp>(op.getLoc(), llvmDstTy, lhs, rhs);
1515+
1516+
cir::PointerType ptrTy = op.getLhs().getType();
1517+
auto typeSize = getTypeSize(ptrTy.getPointee(), *op);
1518+
1519+
// Avoid silly division by 1.
1520+
auto resultVal = diff.getResult();
1521+
if (typeSize != 1) {
1522+
auto typeSizeVal = rewriter.create<mlir::LLVM::ConstantOp>(
1523+
op.getLoc(), llvmDstTy, typeSize);
1524+
1525+
if (dstTy.isUnsigned())
1526+
resultVal =
1527+
rewriter.create<mlir::LLVM::UDivOp>(op.getLoc(), diff, typeSizeVal);
1528+
else
1529+
resultVal =
1530+
rewriter.create<mlir::LLVM::SDivOp>(op.getLoc(), diff, typeSizeVal);
1531+
}
1532+
rewriter.replaceOp(op, resultVal);
1533+
return mlir::success();
1534+
}
1535+
15021536
mlir::LogicalResult CIRToLLVMExpectOpLowering::matchAndRewrite(
15031537
cir::ExpectOp op, OpAdaptor adaptor,
15041538
mlir::ConversionPatternRewriter &rewriter) const {

clang/test/CIR/CodeGen/ptrdiff.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,17 @@ int addrcmp(const void* a, const void* b) {
1616
// LLVM: trunc i64 %[[SUB]] to i32
1717
return *(const void**)a - *(const void**)b;
1818
}
19+
20+
unsigned long long test_ptr_diff(int *a, int* b) {
21+
// CIR-LABEL: test_ptr_diff
22+
// CIR: %[[D:.*]] = cir.ptr_diff{{.*}} : !cir.ptr<!s32i> -> !u64i
23+
// CIR: cir.return %[[D]] : !u64i
24+
25+
// LLVM-LABEL: @_Z13test_ptr_diffPiS_
26+
// LLVM: %[[IA:.*]] = ptrtoint ptr %{{.*}} to i64
27+
// LLVM: %[[IB:.*]] = ptrtoint ptr %{{.*}} to i64
28+
// LLVM: %[[SUB:.*]] = sub i64 %[[IA]], %[[IB]]
29+
// LLVM: %[[Q:.*]] = sdiv exact i64 %[[SUB]], 4
30+
// LLVM: ret i64 %[[Q]]
31+
return a - b;
32+
}

clang/test/CIR/CodeGen/ptrdiff.cpp

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,35 @@
22
// RUN: FileCheck --input-file=%t.cir %s
33

44
typedef unsigned long size_type;
5+
56
size_type size(unsigned long *_start, unsigned long *_finish) {
7+
// CIR-LABEL: cir.func dso_local @_Z4sizePmS_
8+
// CIR: %[[D:.*]] = cir.ptr_diff {{.*}} : !cir.ptr<!u64i> -> !s64i
9+
// CIR: %[[U:.*]] = cir.cast integral %[[D]] : !s64i -> !u64i
10+
// CIR: cir.return %[[U]] : !u64i
11+
12+
// LLVM-LABEL: @_Z4sizePmS_
13+
// LLVM: ptrtoint ptr {{.*}} to i64
14+
// LLVM: ptrtoint ptr {{.*}} to i64
15+
// LLVM: %[[SUB:.*]] = sub i64 {{.*}}, {{.*}}
16+
// LLVM: %[[DIV:.*]] = sdiv exact i64 %[[SUB]], 8
17+
// LLVM: ret i64 %[[DIV]]
618
return static_cast<size_type>(_finish - _start);
719
}
820

9-
// CHECK: cir.func dso_local @_Z4sizePmS_(%arg0: !cir.ptr<!u64i>
10-
// CHECK: %3 = cir.load{{.*}} %1 : !cir.ptr<!cir.ptr<!u64i>>, !cir.ptr<!u64i>
11-
// CHECK: %4 = cir.load{{.*}} %0 : !cir.ptr<!cir.ptr<!u64i>>, !cir.ptr<!u64i>
12-
// CHECK: %5 = cir.ptr_diff %3, %4 : !cir.ptr<!u64i> -> !s64i
13-
// CHECK: %6 = cir.cast integral %5 : !s64i -> !u64i
14-
1521
long add(char *a, char *b) {
22+
// CIR-LABEL: cir.func dso_local @_Z3addPcS_
23+
// CIR: %[[D:.*]] = cir.ptr_diff {{.*}} : !cir.ptr<!s8i> -> !s64i
24+
// CIR: %[[C1:.*]] = cir.const #cir.int<1> : !s32i
25+
// CIR: %[[C1W:.*]] = cir.cast integral %[[C1]] : !s32i -> !s64i
26+
// CIR: %[[S:.*]] = cir.binop(add, %[[D]], %[[C1W]]) : !s64i
27+
// CIR: cir.return %[[S]] : !s64i
28+
29+
// LLVM-LABEL: @_Z3addPcS_
30+
// LLVM: ptrtoint ptr {{.*}} to i64
31+
// LLVM: ptrtoint ptr {{.*}} to i64
32+
// LLVM: %[[SUB:.*]] = sub i64 {{.*}}, {{.*}}
33+
// LLVM: %[[ADD1:.*]] = add i64 %[[SUB]], 1
34+
// LLVM: ret i64 %[[ADD1]]
1635
return a - b + 1;
1736
}
18-
19-
// CHECK: cir.func dso_local @_Z3addPcS_(%arg0: !cir.ptr<!s8i>
20-
// %5 = cir.ptr_diff %3, %4 : !cir.ptr<!s8i> -> !s64i
21-
// %6 = cir.const #cir.int<1> : !s32i
22-
// %7 = cir.cast integral %6 : !s32i -> !s64i
23-
// %8 = cir.binop(add, %5, %7) : !s64i

0 commit comments

Comments
 (0)