Skip to content

Commit

Permalink
[CIR][CodeGen] Use signed type for result of ptrdiff operation. (#355)
Browse files Browse the repository at this point in the history
Before this fix attached test case has been failing due to type mismatch
(signed vs unsigned).
  • Loading branch information
yugr authored and lanza committed Apr 29, 2024
1 parent 7368069 commit bcbfc03
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &context,
// TODO: ConstGlobalsPtrTy
// TODO: ASTAllocaAddressSpace

PtrDiffTy = ::mlir::cir::IntType::get(
builder.getContext(), astCtx.getTargetInfo().getMaxPointerWidth(),
/*isSigned=*/true);

mlir::cir::sob::SignedOverflowBehavior sob;
switch (langOpts.getSignedOverflowBehavior()) {
case clang::LangOptions::SignedOverflowBehaviorTy::SOB_Defined:
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenTypeCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ struct CIRGenTypeCache {
union {
mlir::Type UIntPtrTy;
mlir::Type SizeTy;
mlir::Type PtrDiffTy;
};

mlir::Type PtrDiffTy;

/// void* in address space 0
mlir::cir::PointerType VoidPtrTy;
mlir::cir::PointerType UInt8PtrTy;
Expand Down
15 changes: 13 additions & 2 deletions clang/test/CIR/CodeGen/ptr_diff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,16 @@ size_type size(unsigned long *_start, unsigned long *_finish) {
// CHECK: cir.func @_Z4sizePmS_(%arg0: !cir.ptr<!u64i>
// CHECK: %3 = cir.load %1 : cir.ptr <!cir.ptr<!u64i>>, !cir.ptr<!u64i>
// CHECK: %4 = cir.load %0 : cir.ptr <!cir.ptr<!u64i>>, !cir.ptr<!u64i>
// CHECK: %5 = cir.ptr_diff(%3, %4) : !cir.ptr<!u64i> -> !u64i

// CHECK: %5 = cir.ptr_diff(%3, %4) : !cir.ptr<!u64i> -> !s64i
// CHECK: %6 = cir.cast(integral, %5 : !s64i), !u64i

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

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

0 comments on commit bcbfc03

Please sign in to comment.