diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index cc40bf2fc291..3c49986e14e2 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -372,27 +372,29 @@ def CIR_ObjSizeOp : CIR_Op<"objsize", [Pure]> { //===----------------------------------------------------------------------===// def CIR_PtrDiffOp : CIR_Op<"ptr_diff", [Pure, SameTypeOperands]> { - let summary = "Pointer subtraction arithmetic"; let description = [{ - `cir.ptr_diff` performs a subtraction between two pointer types with the - same element type and produces a `cir::IntType` result. + The cir.ptr_diff operation computes the difference between two pointers that + have the same element type + + The result reflects the ABI-defined size of the pointed-to type. For example, + subtracting two !cir.ptr values may yield 1, representing an 8-byte + difference. In contrast, for pointers to void or function types, a result of + 8 corresponds to an 8-byte difference. - Note that the result considers the pointer size according to the ABI for - the pointee sizes, e.g. the subtraction between two `!cir.ptr` might - yield 1, meaning 8 bytes, whereas for `void` or function type pointees, - yielding 8 means 8 bytes. + Example: ```mlir - %7 = "cir.ptr_diff"(%0, %1) : !cir.ptr -> !u64i + %7 = cir.ptr_diff %0, %1 : !cir.ptr -> !u64i ``` }]; - let results = (outs CIR_AnyFundamentalIntType:$result); let arguments = (ins CIR_PointerType:$lhs, CIR_PointerType:$rhs); + let results = (outs CIR_AnyFundamentalIntType:$result); let assemblyFormat = [{ - `(` $lhs `,` $rhs `)` `:` qualified(type($lhs)) `->` qualified(type($result)) attr-dict + $lhs `,` $rhs `:` qualified(type($lhs)) `->` qualified(type($result)) + attr-dict }]; } diff --git a/clang/test/CIR/CodeGen/ptrdiff.cpp b/clang/test/CIR/CodeGen/ptrdiff.cpp index 7397d4d80ed4..ba54b7a3b11b 100644 --- a/clang/test/CIR/CodeGen/ptrdiff.cpp +++ b/clang/test/CIR/CodeGen/ptrdiff.cpp @@ -9,7 +9,7 @@ size_type size(unsigned long *_start, unsigned long *_finish) { // CHECK: cir.func dso_local @_Z4sizePmS_(%arg0: !cir.ptr // CHECK: %3 = cir.load{{.*}} %1 : !cir.ptr>, !cir.ptr // CHECK: %4 = cir.load{{.*}} %0 : !cir.ptr>, !cir.ptr -// CHECK: %5 = cir.ptr_diff(%3, %4) : !cir.ptr -> !s64i +// CHECK: %5 = cir.ptr_diff %3, %4 : !cir.ptr -> !s64i // CHECK: %6 = cir.cast integral %5 : !s64i -> !u64i long add(char *a, char *b) { @@ -17,7 +17,7 @@ long add(char *a, char *b) { } // CHECK: cir.func dso_local @_Z3addPcS_(%arg0: !cir.ptr -// %5 = cir.ptr_diff(%3, %4) : !cir.ptr -> !s64i +// %5 = cir.ptr_diff %3, %4 : !cir.ptr -> !s64i // %6 = cir.const #cir.int<1> : !s32i // %7 = cir.cast integral %6 : !s32i -> !s64i // %8 = cir.binop(add, %5, %7) : !s64i diff --git a/clang/test/CIR/Lowering/ptrdiff.cir b/clang/test/CIR/Lowering/ptrdiff.cir index 6aacceab15fe..35fddac7356c 100644 --- a/clang/test/CIR/Lowering/ptrdiff.cir +++ b/clang/test/CIR/Lowering/ptrdiff.cir @@ -5,7 +5,7 @@ module { cir.func @foo(%arg0: !cir.ptr, %arg1: !cir.ptr) -> !s32i { - %1 = cir.ptr_diff(%arg0, %arg1) : !cir.ptr -> !u64i + %1 = cir.ptr_diff %arg0, %arg1 : !cir.ptr -> !u64i %2 = cir.cast integral %1 : !u64i -> !s32i cir.return %2 : !s32i } diff --git a/clang/test/CIR/Transforms/lib-opt-find.cpp b/clang/test/CIR/Transforms/lib-opt-find.cpp index 1805ee0932c9..9196dc50c899 100644 --- a/clang/test/CIR/Transforms/lib-opt-find.cpp +++ b/clang/test/CIR/Transforms/lib-opt-find.cpp @@ -52,7 +52,7 @@ unsigned char* test2(unsigned char* first, unsigned char* last, unsigned char v) // CHECK: %[[pattern:.*]] = cir.cast integral %[[load_pattern:.*]] : !u8i -> !s32i // CHECK-NOT: {{.*}} cir.call @_ZSt4findIPhhET_S1_S1_RKT0_( - // CHECK: %[[array_size:.*]] = cir.ptr_diff(%[[last]], %[[first]]) : !cir.ptr -> !u64i + // CHECK: %[[array_size:.*]] = cir.ptr_diff %[[last]], %[[first]] : !cir.ptr -> !u64i // CHECK: %[[result_cast:.*]] = cir.libc.memchr(%[[cast_to_void]], %[[pattern]], %[[array_size]]) // CHECK: %[[memchr_res:.*]] = cir.cast bitcast %[[result_cast]] : !cir.ptr -> !cir.ptr