diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h index 07cebf4948e6..dc61fe08e96f 100644 --- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h +++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h @@ -360,9 +360,11 @@ class CIRBaseBuilderTy : public mlir::OpBuilder { return create(loc, condBuilder, bodyBuilder, stepBuilder); } - mlir::TypedAttr getConstPtrAttr(mlir::Type t, uint64_t v) { + mlir::TypedAttr getConstPtrAttr(mlir::Type t, int64_t v) { + auto val = + mlir::IntegerAttr::get(mlir::IntegerType::get(t.getContext(), 64), v); return mlir::cir::ConstPtrAttr::get(getContext(), - t.cast(), v); + t.cast(), val); } // Creates constant nullptr for pointer type ty. diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td index 6c015d45ec70..8c68c54a4159 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td @@ -250,22 +250,22 @@ def ConstPtrAttr : CIR_Attr<"ConstPtr", "ptr", [TypedAttrInterface]> { let summary = "Holds a constant pointer value"; let parameters = (ins AttributeSelfTypeParameter<"", "::mlir::cir::PointerType">:$type, - "uint64_t":$value); + "mlir::IntegerAttr":$value); let description = [{ A pointer attribute is a literal attribute that represents an integral value of a pointer type. }]; let builders = [ - AttrBuilderWithInferredContext<(ins "Type":$type, "uint64_t":$value), [{ + AttrBuilderWithInferredContext<(ins "Type":$type, "mlir::IntegerAttr":$value), [{ return $_get(type.getContext(), type.cast(), value); }]>, AttrBuilder<(ins "Type":$type, - "uint64_t":$value), [{ + "mlir::IntegerAttr":$value), [{ return $_get($_ctxt, type.cast(), value); }]>, ]; let extraClassDeclaration = [{ - bool isNullValue() const { return getValue() == 0; } + bool isNullValue() const { return getValue().getInt() == 0; } }]; let assemblyFormat = [{ diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h index 351d9549ea2e..ea4676ac193c 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h @@ -146,7 +146,7 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy { mlir::TypedAttr getConstNullPtrAttr(mlir::Type t) { assert(t.isa() && "expected cir.ptr"); - return mlir::cir::ConstPtrAttr::get(getContext(), t, 0); + return getConstPtrAttr(t, 0); } mlir::Attribute getString(llvm::StringRef str, mlir::Type eltTy, @@ -257,7 +257,7 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy { if (auto arrTy = ty.dyn_cast()) return getZeroAttr(arrTy); if (auto ptrTy = ty.dyn_cast()) - return getConstPtrAttr(ptrTy, 0); + return getConstNullPtrAttr(ptrTy); if (auto structTy = ty.dyn_cast()) return getZeroAttr(structTy); if (ty.isa()) { diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp index 6522a756effc..d1284d268a2c 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp @@ -1551,9 +1551,7 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { // Note that DestTy is used as the MLIR type instead of a custom // nullptr type. mlir::Type Ty = CGF.getCIRType(DestTy); - return Builder.create( - CGF.getLoc(E->getExprLoc()), Ty, - mlir::cir::ConstPtrAttr::get(Builder.getContext(), Ty, 0)); + return Builder.getNullPtr(Ty, CGF.getLoc(E->getExprLoc())); } case CK_NullToMemberPointer: { diff --git a/clang/lib/CIR/CodeGen/CIRGenVTables.cpp b/clang/lib/CIR/CodeGen/CIRGenVTables.cpp index fb5a7ac876b9..319adf4619a8 100644 --- a/clang/lib/CIR/CodeGen/CIRGenVTables.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenVTables.cpp @@ -162,9 +162,8 @@ void CIRGenVTables::GenerateClassData(const CXXRecordDecl *RD) { static void AddPointerLayoutOffset(CIRGenModule &CGM, ConstantArrayBuilder &builder, CharUnits offset) { - builder.add(mlir::cir::ConstPtrAttr::get(CGM.getBuilder().getContext(), - CGM.getBuilder().getUInt8PtrTy(), - offset.getQuantity())); + builder.add(CGM.getBuilder().getConstPtrAttr(CGM.getBuilder().getUInt8PtrTy(), + offset.getQuantity())); } static void AddRelativeLayoutOffset(CIRGenModule &CGM, diff --git a/clang/lib/CIR/CodeGen/ConstantInitBuilder.h b/clang/lib/CIR/CodeGen/ConstantInitBuilder.h index 7a32aa591182..1bddd1323473 100644 --- a/clang/lib/CIR/CodeGen/ConstantInitBuilder.h +++ b/clang/lib/CIR/CodeGen/ConstantInitBuilder.h @@ -197,7 +197,9 @@ class ConstantAggregateBuilderBase { /// Add a pointer of a specific type. void addPointer(mlir::cir::PointerType ptrTy, uint64_t value) { - add(mlir::cir::ConstPtrAttr::get(ptrTy.getContext(), ptrTy, value)); + auto val = mlir::IntegerAttr::get( + mlir::IntegerType::get(ptrTy.getContext(), 64), value); + add(mlir::cir::ConstPtrAttr::get(ptrTy.getContext(), ptrTy, val)); } /// Add a bitcast of a value to a specific type. diff --git a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp index b48e1703ca4f..cb9bc69ba6cf 100644 --- a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp @@ -44,9 +44,9 @@ parseFloatLiteral(mlir::AsmParser &parser, mlir::FailureOr &value, mlir::Type ty); static mlir::ParseResult parseConstPtr(mlir::AsmParser &parser, - uint64_t &value); + mlir::IntegerAttr &value); -static void printConstPtr(mlir::AsmPrinter &p, uint64_t value); +static void printConstPtr(mlir::AsmPrinter &p, mlir::IntegerAttr value); #define GET_ATTRDEF_CLASSES #include "clang/CIR/Dialect/IR/CIROpsAttributes.cpp.inc" @@ -220,18 +220,19 @@ void LangAttr::print(AsmPrinter &printer) const { // TODO: Consider encoding the null value differently and use conditional // assembly format instead of custom parsing/printing. -static ParseResult parseConstPtr(AsmParser &parser, uint64_t &value) { +static ParseResult parseConstPtr(AsmParser &parser, mlir::IntegerAttr &value) { if (parser.parseOptionalKeyword("null").succeeded()) { - value = 0; + value = mlir::IntegerAttr::get( + mlir::IntegerType::get(parser.getContext(), 64), 0); return success(); } - return parser.parseInteger(value); + return parser.parseAttribute(value); } -static void printConstPtr(AsmPrinter &p, uint64_t value) { - if (!value) +static void printConstPtr(AsmPrinter &p, mlir::IntegerAttr value) { + if (!value.getInt()) p << "null"; else p << value; diff --git a/clang/lib/CIR/Dialect/Transforms/LibOpt.cpp b/clang/lib/CIR/Dialect/Transforms/LibOpt.cpp index 762ee961bcba..08c2586bc3cf 100644 --- a/clang/lib/CIR/Dialect/Transforms/LibOpt.cpp +++ b/clang/lib/CIR/Dialect/Transforms/LibOpt.cpp @@ -203,8 +203,7 @@ void LibOptPass::xformStdFindIntoMemchr(StdFindOp findOp) { // return result; // else // return last; - auto NullPtr = builder.create( - findOp.getLoc(), first.getType(), ConstPtrAttr::get(first.getType(), 0)); + auto NullPtr = builder.getNullPtr(first.getType(), findOp.getLoc()); auto CmpResult = builder.create( findOp.getLoc(), BoolType::get(builder.getContext()), CmpOpKind::eq, NullPtr.getRes(), MemChrResult); diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index a7c66a7c15f2..debd9dd6dedb 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -188,8 +188,10 @@ lowerCirAttrAsValue(mlir::Operation *parentOp, mlir::cir::ConstPtrAttr ptrAttr, return rewriter.create( loc, converter->convertType(ptrAttr.getType())); } + mlir::DataLayout layout(parentOp->getParentOfType()); mlir::Value ptrVal = rewriter.create( - loc, rewriter.getI64Type(), ptrAttr.getValue()); + loc, rewriter.getIntegerType(layout.getTypeSizeInBits(ptrAttr.getType())), + ptrAttr.getValue().getInt()); return rewriter.create( loc, converter->convertType(ptrAttr.getType()), ptrVal); } @@ -740,10 +742,12 @@ class CIRCastOpLowering : public mlir::OpConversionPattern { return mlir::success(); } case mlir::cir::CastKind::ptr_to_bool: { + auto zero = + mlir::IntegerAttr::get(mlir::IntegerType::get(getContext(), 64), 0); auto null = rewriter.create( src.getLoc(), castOp.getSrc().getType(), mlir::cir::ConstPtrAttr::get(getContext(), castOp.getSrc().getType(), - 0)); + zero)); rewriter.replaceOpWithNewOp( castOp, mlir::cir::BoolType::get(getContext()), mlir::cir::CmpOpKind::ne, castOp.getSrc(), null); diff --git a/clang/test/CIR/CodeGen/constptr.c b/clang/test/CIR/CodeGen/constptr.c index b400cb8c444f..e19f7574566b 100644 --- a/clang/test/CIR/CodeGen/constptr.c +++ b/clang/test/CIR/CodeGen/constptr.c @@ -4,5 +4,5 @@ int *p = (int*)0x1234; -// CIR: cir.global external @p = #cir.ptr<4660> : !cir.ptr +// CIR: cir.global external @p = #cir.ptr<4660 : i64> : !cir.ptr // LLVM: @p = global ptr inttoptr (i64 4660 to ptr) diff --git a/clang/test/CIR/CodeGen/vbase.cpp b/clang/test/CIR/CodeGen/vbase.cpp index a966f82069c6..b32fb0eb59ab 100644 --- a/clang/test/CIR/CodeGen/vbase.cpp +++ b/clang/test/CIR/CodeGen/vbase.cpp @@ -15,7 +15,7 @@ void ppp() { B b; } // Vtable definition for B -// CIR: cir.global linkonce_odr @_ZTV1B = #cir.vtable<{#cir.const_array<[#cir.ptr<12> : !cir.ptr, #cir.ptr : !cir.ptr, #cir.global_view<@_ZTI1B> : !cir.ptr]> : !cir.array x 3>}> +// CIR: cir.global linkonce_odr @_ZTV1B = #cir.vtable<{#cir.const_array<[#cir.ptr<12 : i64> : !cir.ptr, #cir.ptr : !cir.ptr, #cir.global_view<@_ZTI1B> : !cir.ptr]> : !cir.array x 3>}> // VTT for B. // CIR: cir.global linkonce_odr @_ZTT1B = #cir.const_array<[#cir.global_view<@_ZTV1B, [0 : i32, 0 : i32, 3 : i32]> : !cir.ptr]> : !cir.array x 1> diff --git a/clang/test/CIR/IR/constptrattr.cir b/clang/test/CIR/IR/constptrattr.cir index 30b79a882ac1..98b215caacf7 100644 --- a/clang/test/CIR/IR/constptrattr.cir +++ b/clang/test/CIR/IR/constptrattr.cir @@ -2,7 +2,9 @@ !s32i = !cir.int -cir.global external @const_ptr = #cir.ptr<4660> : !cir.ptr -// CHECK: cir.global external @const_ptr = #cir.ptr<4660> : !cir.ptr +cir.global external @const_ptr = #cir.ptr<4660 : i64> : !cir.ptr +// CHECK: cir.global external @const_ptr = #cir.ptr<4660 : i64> : !cir.ptr +cir.global external @signed_ptr = #cir.ptr<-1> : !cir.ptr +// CHECK: cir.global external @signed_ptr = #cir.ptr<-1 : i64> : !cir.ptr cir.global external @null_ptr = #cir.ptr : !cir.ptr // CHECK: cir.global external @null_ptr = #cir.ptr : !cir.ptr diff --git a/clang/test/CIR/Lowering/types.cir b/clang/test/CIR/Lowering/types.cir index 5e5be9192e8a..f91f25cb5e41 100644 --- a/clang/test/CIR/Lowering/types.cir +++ b/clang/test/CIR/Lowering/types.cir @@ -2,7 +2,11 @@ // RUN: FileCheck --input-file=%t.mlir %s !void = !cir.void +!u8i = !cir.int module { + cir.global external @testVTable = #cir.vtable<{#cir.const_array<[#cir.ptr<-8> : !cir.ptr]> : !cir.array x 1>}> : !cir.struct x 1>}> + // CHECK: llvm.mlir.constant(-8 : i64) : i64 + // CHECK: llvm.inttoptr %{{[0-9]+}} : i64 to !llvm.ptr cir.func @testTypeLowering() { // Should lower void pointers as opaque pointers. %0 = cir.const #cir.ptr : !cir.ptr @@ -11,4 +15,4 @@ module { // CHECK: llvm.mlir.zero : !llvm.ptr cir.return } -} +} \ No newline at end of file