Skip to content

Commit 507edc2

Browse files
committed
Apply test and doc feedback
1 parent de497b2 commit 507edc2

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4052,13 +4052,19 @@ def CIR_PtrDiffOp : CIR_Op<"ptr_diff", [Pure, SameTypeOperands]> {
40524052
let summary = "Pointer subtraction arithmetic";
40534053
let description = [{
40544054
The cir.ptr_diff operation computes the difference between two pointers that
4055-
have the same element type
4055+
have the same element type.
40564056

40574057
The result reflects the ABI-defined size of the pointed-to type. For example,
40584058
subtracting two !cir.ptr<!u64i> values may yield 1, representing an 8-byte
40594059
difference. In contrast, for pointers to void or function types, a result of
40604060
8 corresponds to an 8-byte difference.
40614061

4062+
For pointers to types whose size are not aligned with the target data
4063+
layout, the size is generally rounded to the next power of 2 bits. For
4064+
example, subtracting two !cir.ptr<!s24i> values for the _BitInt(24) type may
4065+
yield 1, representing a 4-byte difference (as opposed to a 3-byte
4066+
difference).
4067+
40624068
Example:
40634069

40644070
```mlir

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,12 +1531,17 @@ mlir::LogicalResult CIRToLLVMPtrDiffOpLowering::matchAndRewrite(
15311531
auto typeSizeVal = rewriter.create<mlir::LLVM::ConstantOp>(
15321532
op.getLoc(), llvmDstTy, typeSize);
15331533

1534-
if (dstTy.isUnsigned())
1535-
resultVal =
1534+
if (dstTy.isUnsigned()) {
1535+
auto uDiv =
15361536
rewriter.create<mlir::LLVM::UDivOp>(op.getLoc(), diff, typeSizeVal);
1537-
else
1538-
resultVal =
1537+
uDiv.setIsExact(true);
1538+
resultVal = uDiv.getResult();
1539+
} else {
1540+
auto sDiv =
15391541
rewriter.create<mlir::LLVM::SDivOp>(op.getLoc(), diff, typeSizeVal);
1542+
sDiv.setIsExact(true);
1543+
resultVal = sDiv.getResult();
1544+
}
15401545
}
15411546
rewriter.replaceOp(op, resultVal);
15421547
return mlir::success();

clang/test/CIR/CodeGen/ptrdiff.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
22
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
3-
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
4-
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
4+
// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s
5+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
6+
// RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s
57

68
int addrcmp(const void* a, const void* b) {
79
// CIR-LABEL: addrcmp
@@ -14,6 +16,13 @@ int addrcmp(const void* a, const void* b) {
1416
// LLVM: %[[SUB:.*]] = sub i64 %[[PTR_A]], %[[PTR_B]]
1517
// LLVM-NOT: sdiv
1618
// LLVM: trunc i64 %[[SUB]] to i32
19+
20+
// OGCG-LABEL: define dso_local i32 @addrcmp(
21+
// OGCG: %[[PTR_A:.*]] = ptrtoint ptr {{.*}} to i64
22+
// OGCG: %[[PTR_B:.*]] = ptrtoint ptr {{.*}} to i64
23+
// OGCG: %[[SUB:.*]] = sub i64 %[[PTR_A]], %[[PTR_B]]
24+
// OGCG-NOT: sdiv
25+
// OGCG: trunc i64 %[[SUB]] to i32
1726
return *(const void**)a - *(const void**)b;
1827
}
1928

@@ -27,9 +36,16 @@ unsigned long long test_ptr_diff(int *a, int* b) {
2736
// LLVM: %[[IA:.*]] = ptrtoint ptr %{{.*}} to i64
2837
// LLVM: %[[IB:.*]] = ptrtoint ptr %{{.*}} to i64
2938
// LLVM: %[[SUB:.*]] = sub i64 %[[IA]], %[[IB]]
30-
// LLVM: %[[Q:.*]] = sdiv{{( exact)?}} i64 %[[SUB]], 4
39+
// LLVM: %[[Q:.*]] = sdiv exact i64 %[[SUB]], 4
3140
// LLVM: store i64 %[[Q]], ptr %[[RETADDR:.*]], align
3241
// LLVM: %[[RETLOAD:.*]] = load i64, ptr %[[RETADDR]], align
3342
// LLVM: ret i64 %[[RETLOAD]]
43+
44+
// OGCG-LABEL: define dso_local i64 @test_ptr_diff(
45+
// OGCG: %[[IA:.*]] = ptrtoint ptr %{{.*}} to i64
46+
// OGCG: %[[IB:.*]] = ptrtoint ptr %{{.*}} to i64
47+
// OGCG: %[[SUB:.*]] = sub i64 %[[IA]], %[[IB]]
48+
// OGCG: %[[Q:.*]] = sdiv exact i64 %[[SUB]], 4
49+
// OGCG: ret i64 %[[Q]]
3450
return a - b;
35-
}
51+
}

clang/test/CIR/CodeGen/ptrdiff.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
22
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR
3+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
4+
// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=LLVM
5+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
6+
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=OGCG
37

48
typedef unsigned long size_type;
59

@@ -9,5 +13,21 @@ size_type size(unsigned long *_start, unsigned long *_finish) {
913
// CIR: %[[U:.*]] = cir.cast integral %[[D]] : !s64i -> !u64i
1014
// CIR: cir.return {{.*}} : !u64i
1115

16+
// LLVM-LABEL: define dso_local {{.*}}i64 @_Z4sizePmS_(
17+
// LLVM: %[[IA:.*]] = ptrtoint ptr %{{.*}} to i64
18+
// LLVM: %[[IB:.*]] = ptrtoint ptr %{{.*}} to i64
19+
// LLVM: %[[SUB:.*]] = sub i64 %[[IA]], %[[IB]]
20+
// LLVM: %[[Q:.*]] = sdiv exact i64 %[[SUB]], 8
21+
// LLVM: store i64 %[[Q]], ptr %[[RETADDR:.*]], align
22+
// LLVM: %[[RET:.*]] = load i64, ptr %[[RETADDR]], align
23+
// LLVM: ret i64 %[[RET]]
24+
25+
// OGCG-LABEL: define dso_local {{.*}}i64 @_Z4sizePmS_(
26+
// OGCG: %[[IA:.*]] = ptrtoint ptr %{{.*}} to i64
27+
// OGCG: %[[IB:.*]] = ptrtoint ptr %{{.*}} to i64
28+
// OGCG: %[[SUB:.*]] = sub i64 %[[IA]], %[[IB]]
29+
// OGCG: %[[Q:.*]] = sdiv exact i64 %[[SUB]], 8
30+
// OGCG: ret i64 %[[Q]]
31+
1232
return static_cast<size_type>(_finish - _start);
13-
}
33+
}

0 commit comments

Comments
 (0)