Skip to content

Commit bdd7436

Browse files
committed
[CIR] Clean up ptr_diff operation assembly format and description
1 parent e964257 commit bdd7436

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,27 +372,29 @@ def CIR_ObjSizeOp : CIR_Op<"objsize", [Pure]> {
372372
//===----------------------------------------------------------------------===//
373373

374374
def CIR_PtrDiffOp : CIR_Op<"ptr_diff", [Pure, SameTypeOperands]> {
375-
376375
let summary = "Pointer subtraction arithmetic";
377376
let description = [{
378-
`cir.ptr_diff` performs a subtraction between two pointer types with the
379-
same element type and produces a `cir::IntType` result.
377+
The cir.ptr_diff operation computes the difference between two pointers that
378+
have the same element type
379+
380+
The result reflects the ABI-defined size of the pointed-to type. For example,
381+
subtracting two !cir.ptr<!u64i> values may yield 1, representing an 8-byte
382+
difference. In contrast, for pointers to void or function types, a result of
383+
8 corresponds to an 8-byte difference.
380384

381-
Note that the result considers the pointer size according to the ABI for
382-
the pointee sizes, e.g. the subtraction between two `!cir.ptr<!u64i>` might
383-
yield 1, meaning 8 bytes, whereas for `void` or function type pointees,
384-
yielding 8 means 8 bytes.
385+
Example:
385386

386387
```mlir
387-
%7 = "cir.ptr_diff"(%0, %1) : !cir.ptr<!u64i> -> !u64i
388+
%7 = cir.ptr_diff %0, %1 : !cir.ptr<!u64i> -> !u64i
388389
```
389390
}];
390391

391-
let results = (outs CIR_AnyFundamentalIntType:$result);
392392
let arguments = (ins CIR_PointerType:$lhs, CIR_PointerType:$rhs);
393+
let results = (outs CIR_AnyFundamentalIntType:$result);
393394

394395
let assemblyFormat = [{
395-
`(` $lhs `,` $rhs `)` `:` qualified(type($lhs)) `->` qualified(type($result)) attr-dict
396+
$lhs `,` $rhs `:` qualified(type($lhs)) `->` qualified(type($result))
397+
attr-dict
396398
}];
397399
}
398400

clang/test/CIR/CodeGen/ptrdiff.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ size_type size(unsigned long *_start, unsigned long *_finish) {
99
// CHECK: cir.func dso_local @_Z4sizePmS_(%arg0: !cir.ptr<!u64i>
1010
// CHECK: %3 = cir.load{{.*}} %1 : !cir.ptr<!cir.ptr<!u64i>>, !cir.ptr<!u64i>
1111
// 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
12+
// CHECK: %5 = cir.ptr_diff %3, %4 : !cir.ptr<!u64i> -> !s64i
1313
// CHECK: %6 = cir.cast integral %5 : !s64i -> !u64i
1414

1515
long add(char *a, char *b) {
1616
return a - b + 1;
1717
}
1818

1919
// CHECK: cir.func dso_local @_Z3addPcS_(%arg0: !cir.ptr<!s8i>
20-
// %5 = cir.ptr_diff(%3, %4) : !cir.ptr<!s8i> -> !s64i
20+
// %5 = cir.ptr_diff %3, %4 : !cir.ptr<!s8i> -> !s64i
2121
// %6 = cir.const #cir.int<1> : !s32i
2222
// %7 = cir.cast integral %6 : !s32i -> !s64i
2323
// %8 = cir.binop(add, %5, %7) : !s64i

clang/test/CIR/Lowering/ptrdiff.cir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
module {
77
cir.func @foo(%arg0: !cir.ptr<!s32i>, %arg1: !cir.ptr<!s32i>) -> !s32i {
8-
%1 = cir.ptr_diff(%arg0, %arg1) : !cir.ptr<!s32i> -> !u64i
8+
%1 = cir.ptr_diff %arg0, %arg1 : !cir.ptr<!s32i> -> !u64i
99
%2 = cir.cast integral %1 : !u64i -> !s32i
1010
cir.return %2 : !s32i
1111
}

clang/test/CIR/Transforms/lib-opt-find.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ unsigned char* test2(unsigned char* first, unsigned char* last, unsigned char v)
5252
// CHECK: %[[pattern:.*]] = cir.cast integral %[[load_pattern:.*]] : !u8i -> !s32i
5353

5454
// CHECK-NOT: {{.*}} cir.call @_ZSt4findIPhhET_S1_S1_RKT0_(
55-
// CHECK: %[[array_size:.*]] = cir.ptr_diff(%[[last]], %[[first]]) : !cir.ptr<!u8i> -> !u64i
55+
// CHECK: %[[array_size:.*]] = cir.ptr_diff %[[last]], %[[first]] : !cir.ptr<!u8i> -> !u64i
5656

5757
// CHECK: %[[result_cast:.*]] = cir.libc.memchr(%[[cast_to_void]], %[[pattern]], %[[array_size]])
5858
// CHECK: %[[memchr_res:.*]] = cir.cast bitcast %[[result_cast]] : !cir.ptr<!void> -> !cir.ptr<!u8i>

0 commit comments

Comments
 (0)