diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h index ac9aeabe1f4b..27f7699ce7b7 100644 --- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h +++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h @@ -156,17 +156,20 @@ class CIRBaseBuilderTy : public mlir::OpBuilder { llvm_unreachable("Zero initializer for given type is NYI"); } + using mlir::OpBuilder::getIntegerAttr; + + mlir::IntegerAttr getIntegerAttr(const llvm::APInt &value) { + mlir::Type intType = getIntegerType(value.getBitWidth()); + return getIntegerAttr(intType, value); + } + cir::LoadOp createLoad(mlir::Location loc, mlir::Value ptr, bool isVolatile = false, bool isNontemporal = false, uint64_t alignment = 0) { - mlir::IntegerAttr intAttr; - if (alignment) - intAttr = mlir::IntegerAttr::get( - mlir::IntegerType::get(ptr.getContext(), 64), alignment); - + mlir::IntegerAttr alignmentAttr = getAlignmentAttr(alignment); return create(loc, ptr, /*isDeref=*/false, isVolatile, isNontemporal, - /*alignment=*/intAttr, + /*alignment=*/alignmentAttr, /*mem_order=*/ cir::MemOrderAttr{}, /*tbaa=*/cir::TBAAAttr{}); @@ -373,9 +376,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder { mlir::Type type, llvm::StringRef name, clang::CharUnits alignment, mlir::Value dynAllocSize) { - auto alignmentIntAttr = getSizeFromCharUnits(getContext(), alignment); - return createAlloca(loc, addrType, type, name, alignmentIntAttr, - dynAllocSize); + mlir::IntegerAttr alignmentAttr = getAlignmentAttr(alignment); + return createAlloca(loc, addrType, type, name, alignmentAttr, dynAllocSize); } mlir::Value createAlloca(mlir::Location loc, cir::PointerType addrType, @@ -387,8 +389,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder { mlir::Value createAlloca(mlir::Location loc, cir::PointerType addrType, mlir::Type type, llvm::StringRef name, clang::CharUnits alignment) { - auto alignmentIntAttr = getSizeFromCharUnits(getContext(), alignment); - return createAlloca(loc, addrType, type, name, alignmentIntAttr); + mlir::IntegerAttr alignmentAttr = getAlignmentAttr(alignment); + return createAlloca(loc, addrType, type, name, alignmentAttr); } mlir::Value createGetGlobal(mlir::Location loc, cir::GlobalOp global, @@ -581,13 +583,28 @@ class CIRBaseBuilderTy : public mlir::OpBuilder { return OpBuilder::InsertPoint(block, block->begin()); }; - mlir::IntegerAttr getSizeFromCharUnits(mlir::MLIRContext *ctx, - clang::CharUnits size) { - // Note that mlir::IntegerType is used instead of cir::IntType here - // because we don't need sign information for this to be useful, so keep - // it simple. - return mlir::IntegerAttr::get(mlir::IntegerType::get(ctx, 64), - size.getQuantity()); + // + // Alignement and size helpers + // + + // Note that mlir::IntegerType is used instead of cir::IntType here because we + // don't need sign information for these to be useful, so keep it simple. + + // Fot 0 alignment, return an empty attribute. + mlir::IntegerAttr getAlignmentAttr(clang::CharUnits alignment) { + return getAlignmentAttr(alignment.getQuantity()); + } + + mlir::IntegerAttr getAlignmentAttr(llvm::Align alignment) { + return getAlignmentAttr(alignment.value()); + } + + mlir::IntegerAttr getAlignmentAttr(int64_t alignment) { + return alignment ? getI64IntegerAttr(alignment) : mlir::IntegerAttr(); + } + + mlir::IntegerAttr getSizeFromCharUnits(clang::CharUnits size) { + return getI64IntegerAttr(size.getQuantity()); } /// Create a do-while operation. @@ -616,9 +633,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder { } mlir::TypedAttr getConstPtrAttr(mlir::Type t, int64_t v) { - auto val = - mlir::IntegerAttr::get(mlir::IntegerType::get(t.getContext(), 64), v); - return cir::ConstPtrAttr::get(t, val); + return cir::ConstPtrAttr::get(t, getI64IntegerAttr(v)); } mlir::TypedAttr getConstNullPtrAttr(mlir::Type t) { diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h index 4a68587fc224..02c55f932329 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h @@ -151,9 +151,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy { llvm::ArrayRef indices) { llvm::SmallVector attrs; for (auto ind : indices) { - auto a = - mlir::IntegerAttr::get(mlir::IntegerType::get(getContext(), 64), ind); - attrs.push_back(a); + attrs.push_back(getI64IntegerAttr(ind)); } mlir::ArrayAttr arAttr = mlir::ArrayAttr::get(getContext(), attrs); @@ -938,13 +936,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy { clang::CharUnits align = clang::CharUnits::One(), bool isVolatile = false, bool isNontemporal = false, cir::MemOrderAttr order = {}) { - llvm::MaybeAlign mayAlign = align.getAsAlign(); - mlir::IntegerAttr alignAttr; - if (mayAlign) { - uint64_t alignment = mayAlign ? mayAlign->value() : 0; - alignAttr = mlir::IntegerAttr::get( - mlir::IntegerType::get(dst.getContext(), 64), alignment); - } + mlir::IntegerAttr alignAttr = getAlignmentAttr(align); return CIRBaseBuilderTy::createStore(loc, val, dst, isVolatile, isNontemporal, alignAttr, order); } diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index 738351ac41ad..83134995967f 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -1706,10 +1706,9 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, E->getArg(1)->getExprLoc(), FD, 1); uint64_t size = E->getArg(2)->EvaluateKnownConstInt(getContext()).getZExtValue(); - builder.create( - getLoc(E->getSourceRange()), dest.getPointer(), src.getPointer(), - mlir::IntegerAttr::get(mlir::IntegerType::get(builder.getContext(), 64), - size)); + builder.create(getLoc(E->getSourceRange()), + dest.getPointer(), src.getPointer(), + builder.getI64IntegerAttr(size)); // __builtin_memcpy_inline has no return value return RValue::get(nullptr); } @@ -1788,10 +1787,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, E->getArg(2)->EvaluateKnownConstInt(getContext()).getZExtValue(); emitNonNullArgCheck(RValue::get(Dest.getPointer()), E->getArg(0)->getType(), E->getArg(0)->getExprLoc(), FD, 0); - builder.createMemSetInline( - getLoc(E->getSourceRange()), Dest.getPointer(), ByteVal, - mlir::IntegerAttr::get(mlir::IntegerType::get(builder.getContext(), 64), - size)); + builder.createMemSetInline(getLoc(E->getSourceRange()), Dest.getPointer(), + ByteVal, builder.getI64IntegerAttr(size)); // __builtin_memset_inline has no return value return RValue::get(nullptr); } diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.h b/clang/lib/CIR/CodeGen/CIRGenFunction.h index e1785f9d1009..2c7d16592f75 100644 --- a/clang/lib/CIR/CodeGen/CIRGenFunction.h +++ b/clang/lib/CIR/CodeGen/CIRGenFunction.h @@ -1269,12 +1269,11 @@ class CIRGenFunction : public CIRGenTypeCache { { mlir::OpBuilder::InsertionGuard guard(builder); builder.restoreInsertionPoint(OutermostConditional->getInsertPoint()); - builder.createStore( - value.getLoc(), value, addr, - /*isVolatile=*/false, /*isNontemporal=*/false, - mlir::IntegerAttr::get( - mlir::IntegerType::get(value.getContext(), 64), - (uint64_t)addr.getAlignment().getAsAlign().value())); + mlir::IntegerAttr alignmentAttr = + builder.getAlignmentAttr(addr.getAlignment()); + builder.createStore(value.getLoc(), value, addr, + /*isVolatile=*/false, /*isNontemporal=*/false, + alignmentAttr); } } diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 2555513c2a67..b36185f4f36a 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -3342,7 +3342,7 @@ void CIRGenModule::emitDeferred(unsigned recursionLimit) { } mlir::IntegerAttr CIRGenModule::getSize(CharUnits size) { - return builder.getSizeFromCharUnits(&getMLIRContext(), size); + return builder.getSizeFromCharUnits(size); } mlir::Operation * @@ -4214,10 +4214,7 @@ mlir::ArrayAttr CIRGenModule::emitAnnotationArgs(const AnnotateAttr *attr) { // Handle case which can be evaluated to some numbers, not only literals const auto &ap = ce.getAPValueResult(); if (ap.isInt()) { - args.push_back(mlir::IntegerAttr::get( - mlir::IntegerType::get(&getMLIRContext(), - ap.getInt().getBitWidth()), - ap.getInt())); + args.push_back(builder.getIntegerAttr(ap.getInt())); } else { llvm_unreachable("NYI like float, fixed-point, array..."); } diff --git a/clang/lib/CIR/CodeGen/ConstantInitBuilder.h b/clang/lib/CIR/CodeGen/ConstantInitBuilder.h index dcd5c0e00de8..eb181cba809f 100644 --- a/clang/lib/CIR/CodeGen/ConstantInitBuilder.h +++ b/clang/lib/CIR/CodeGen/ConstantInitBuilder.h @@ -193,8 +193,7 @@ class ConstantAggregateBuilderBase { /// Add a pointer of a specific type. void addPointer(cir::PointerType ptrTy, uint64_t value) { - auto val = mlir::IntegerAttr::get( - mlir::IntegerType::get(ptrTy.getContext(), 64), value); + mlir::IntegerAttr val = Builder.builder.getI64IntegerAttr(value); add(cir::ConstPtrAttr::get(ptrTy, val)); } diff --git a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp index 9f50562a2a4a..a06348e2ab18 100644 --- a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp @@ -186,8 +186,7 @@ LogicalResult OptInfoAttr::verify(function_ref emitError, static ParseResult parseConstPtr(AsmParser &parser, mlir::IntegerAttr &value) { if (parser.parseOptionalKeyword("null").succeeded()) { - value = mlir::IntegerAttr::get( - mlir::IntegerType::get(parser.getContext(), 64), 0); + value = parser.getBuilder().getI64IntegerAttr(0); return success(); } diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 776076888575..550c15d73862 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -960,10 +960,9 @@ mlir::LogicalResult CIRToLLVMPtrStrideOpLowering::matchAndRewrite( // Rewrite the sub in front of extensions/trunc if (rewriteSub) { index = rewriter.create( - index.getLoc(), index.getType(), - rewriter.create( - index.getLoc(), index.getType(), - mlir::IntegerAttr::get(index.getType(), 0)), + index.getLoc(), + rewriter.create(index.getLoc(), + index.getType(), 0), index); rewriter.eraseOp(sub); } @@ -1320,8 +1319,7 @@ mlir::LogicalResult CIRToLLVMCastOpLowering::matchAndRewrite( return mlir::success(); } case cir::CastKind::ptr_to_bool: { - auto zero = - mlir::IntegerAttr::get(mlir::IntegerType::get(getContext(), 64), 0); + auto zero = rewriter.getI64IntegerAttr(0); auto null = rewriter.create( src.getLoc(), cir::ConstPtrAttr::get(castOp.getSrc().getType(), zero)); rewriter.replaceOpWithNewOp( @@ -2035,8 +2033,7 @@ mlir::LogicalResult CIRToLLVMVecShuffleDynamicOpLowering::matchAndRewrite( uint64_t numElements = mlir::cast(op.getVec().getType()).getSize(); mlir::Value maskValue = rewriter.create( - loc, llvmIndexType, - mlir::IntegerAttr::get(llvmIndexType, numElements - 1)); + loc, llvmIndexType, numElements - 1); mlir::Value maskVector = rewriter.create(loc, llvmIndexVecType); for (uint64_t i = 0; i < numElements; ++i) { @@ -2607,16 +2604,14 @@ mlir::LogicalResult CIRToLLVMUnaryOpLowering::matchAndRewrite( switch (op.getKind()) { case cir::UnaryOpKind::Inc: { assert(!IsVector && "++ not allowed on vector types"); - auto One = rewriter.create( - loc, llvmType, mlir::IntegerAttr::get(llvmType, 1)); + auto One = rewriter.create(loc, llvmType, 1); rewriter.replaceOpWithNewOp( op, llvmType, adaptor.getInput(), One, overflowFlags); return mlir::success(); } case cir::UnaryOpKind::Dec: { assert(!IsVector && "-- not allowed on vector types"); - auto One = rewriter.create( - loc, llvmType, mlir::IntegerAttr::get(llvmType, 1)); + auto One = rewriter.create(loc, llvmType, 1); rewriter.replaceOpWithNewOp( op, llvmType, adaptor.getInput(), One, overflowFlags); return mlir::success(); @@ -2630,10 +2625,9 @@ mlir::LogicalResult CIRToLLVMUnaryOpLowering::matchAndRewrite( if (IsVector) Zero = rewriter.create(loc, llvmType); else - Zero = rewriter.create( - loc, llvmType, mlir::IntegerAttr::get(llvmType, 0)); + Zero = rewriter.create(loc, llvmType, 0); rewriter.replaceOpWithNewOp( - op, llvmType, Zero, adaptor.getInput(), overflowFlags); + op, Zero, adaptor.getInput(), overflowFlags); return mlir::success(); } case cir::UnaryOpKind::Not: { @@ -2644,8 +2638,8 @@ mlir::LogicalResult CIRToLLVMUnaryOpLowering::matchAndRewrite( // done. It requires a series of insertelement ops. mlir::Type llvmElementType = getTypeConverter()->convertType(elementType); - auto MinusOneInt = rewriter.create( - loc, llvmElementType, mlir::IntegerAttr::get(llvmElementType, -1)); + auto MinusOneInt = + rewriter.create(loc, llvmElementType, -1); minusOne = rewriter.create(loc, llvmType); auto NumElements = mlir::dyn_cast(type).getSize(); for (uint64_t i = 0; i < NumElements; ++i) { @@ -2655,11 +2649,10 @@ mlir::LogicalResult CIRToLLVMUnaryOpLowering::matchAndRewrite( loc, minusOne, MinusOneInt, indexValue); } } else { - minusOne = rewriter.create( - loc, llvmType, mlir::IntegerAttr::get(llvmType, -1)); + minusOne = rewriter.create(loc, llvmType, -1); } - rewriter.replaceOpWithNewOp( - op, llvmType, adaptor.getInput(), minusOne); + rewriter.replaceOpWithNewOp(op, adaptor.getInput(), + minusOne); return mlir::success(); } } @@ -2707,9 +2700,8 @@ mlir::LogicalResult CIRToLLVMUnaryOpLowering::matchAndRewrite( case cir::UnaryOpKind::Not: assert(!IsVector && "NYI: op! on vector mask"); rewriter.replaceOpWithNewOp( - op, llvmType, adaptor.getInput(), - rewriter.create( - loc, llvmType, mlir::IntegerAttr::get(llvmType, 1))); + op, adaptor.getInput(), + rewriter.create(loc, llvmType, 1)); return mlir::success(); default: return op.emitError() @@ -3671,14 +3663,14 @@ mlir::LogicalResult CIRToLLVMPtrDiffOpLowering::matchAndRewrite( auto resultVal = diff.getResult(); if (typeSize != 1) { auto typeSizeVal = rewriter.create( - op.getLoc(), llvmDstTy, mlir::IntegerAttr::get(llvmDstTy, typeSize)); + op.getLoc(), llvmDstTy, typeSize); if (dstTy.isUnsigned()) - resultVal = rewriter.create(op.getLoc(), llvmDstTy, - diff, typeSizeVal); + resultVal = + rewriter.create(op.getLoc(), diff, typeSizeVal); else - resultVal = rewriter.create(op.getLoc(), llvmDstTy, - diff, typeSizeVal); + resultVal = + rewriter.create(op.getLoc(), diff, typeSizeVal); } rewriter.replaceOp(op, resultVal); return mlir::success(); diff --git a/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRLoopToSCF.cpp b/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRLoopToSCF.cpp index 69d58fea2703..acc2630db91c 100644 --- a/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRLoopToSCF.cpp +++ b/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRLoopToSCF.cpp @@ -232,7 +232,7 @@ mlir::Value SCFLoop::plusConstant(mlir::Value V, mlir::Location loc, int addend) { auto type = V.getType(); auto c1 = rewriter->create( - loc, type, mlir::IntegerAttr::get(type, addend)); + loc, mlir::IntegerAttr::get(type, addend)); return rewriter->create(loc, V, c1); } @@ -285,7 +285,7 @@ void SCFLoop::transferToSCFForOp() { auto loc = forOp.getLoc(); auto type = lb.getType(); auto step = rewriter->create( - loc, type, mlir::IntegerAttr::get(type, getStep())); + loc, mlir::IntegerAttr::get(type, getStep())); auto scfForOp = rewriter->create(loc, lb, ub, step); SmallVector bbArg; rewriter->eraseOp(&scfForOp.getBody()->back()); diff --git a/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp b/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp index 7a0a9bceac23..bca5354dff8c 100644 --- a/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp +++ b/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp @@ -571,13 +571,13 @@ class CIRUnaryOpLowering : public mlir::OpConversionPattern { switch (op.getKind()) { case cir::UnaryOpKind::Inc: { auto One = rewriter.create( - op.getLoc(), type, mlir::IntegerAttr::get(type, 1)); + op.getLoc(), mlir::IntegerAttr::get(type, 1)); rewriter.replaceOpWithNewOp(op, type, input, One); break; } case cir::UnaryOpKind::Dec: { auto One = rewriter.create( - op.getLoc(), type, mlir::IntegerAttr::get(type, 1)); + op.getLoc(), mlir::IntegerAttr::get(type, 1)); rewriter.replaceOpWithNewOp(op, type, input, One); break; } @@ -587,13 +587,13 @@ class CIRUnaryOpLowering : public mlir::OpConversionPattern { } case cir::UnaryOpKind::Minus: { auto Zero = rewriter.create( - op.getLoc(), type, mlir::IntegerAttr::get(type, 0)); + op.getLoc(), mlir::IntegerAttr::get(type, 0)); rewriter.replaceOpWithNewOp(op, type, Zero, input); break; } case cir::UnaryOpKind::Not: { auto MinusOne = rewriter.create( - op.getLoc(), type, mlir::IntegerAttr::get(type, -1)); + op.getLoc(), mlir::IntegerAttr::get(type, -1)); rewriter.replaceOpWithNewOp(op, type, MinusOne, input); break;