17#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
50packArgsIntoNVPTXFormatBuffer(cir::OffloadPrintfOp op, mlir::ValueRange args,
51 mlir::ConversionPatternRewriter &rewriter,
52 const mlir::DataLayout &dataLayout) {
53 mlir::Location loc = op.getLoc();
54 auto ptrTy = mlir::LLVM::LLVMPointerType::get(rewriter.getContext());
59 return mlir::LLVM::ZeroOp::create(rewriter, loc, ptrTy);
64 auto allocaTy = mlir::LLVM::LLVMStructType::getLiteral(
65 rewriter.getContext(), llvm::to_vector(args.getTypes()));
71 mlir::OpBuilder::InsertionGuard guard(rewriter);
72 auto fn = op->getParentOfType<mlir::LLVM::LLVMFuncOp>();
73 assert(fn &&
"lowering expects printf to be inside a function");
74 rewriter.setInsertionPointToStart(&fn.getBody().front());
76 mlir::LLVM::ConstantOp::create(rewriter, loc, rewriter.getI32Type(), 1);
78 mlir::LLVM::AllocaOp::create(rewriter, loc, ptrTy, allocaTy, one,
79 dataLayout.getTypeABIAlignment(allocaTy));
83 mlir::LLVM::GEPNoWrapFlags flags =
84 mlir::LLVM::GEPNoWrapFlags::inbounds | mlir::LLVM::GEPNoWrapFlags::nuw;
85 for (
auto [i, arg] : llvm::enumerate(args)) {
86 mlir::Value
member = mlir::LLVM::GEPOp::create(
87 rewriter, loc, ptrTy, allocaTy, alloca,
88 llvm::ArrayRef<mlir::LLVM::GEPArg>{0,
static_cast<int32_t>(i)}, flags);
89 mlir::LLVM::StoreOp::create(rewriter, loc, arg, member,
90 dataLayout.getTypeABIAlignment(
arg.getType()));
102 mlir::ValueRange args,
103 mlir::ConversionPatternRewriter &rewriter,
104 const mlir::DataLayout &dataLayout,
105 mlir::SymbolTableCollection &symbolTables) {
106 mlir::Value packedData =
107 packArgsIntoNVPTXFormatBuffer(op, args, rewriter, dataLayout);
110 auto ptrTy = mlir::LLVM::LLVMPointerType::get(rewriter.getContext());
111 mlir::Type i32Ty = rewriter.getI32Type();
112 const llvm::StringRef fnName =
"vprintf";
114 rewriter, symbolTables, op, fnName,
115 mlir::LLVM::LLVMFunctionType::get(i32Ty, {ptrTy, ptrTy}));
118 if (format.getType() != ptrTy)
119 format = mlir::LLVM::AddrSpaceCastOp::create(rewriter, op.getLoc(), ptrTy,
121 rewriter.replaceOpWithNewOp<mlir::LLVM::CallOp>(
122 op, mlir::TypeRange{i32Ty}, fnName, mlir::ValueRange{format, packedData});
123 return mlir::success();
void createLLVMFuncOpIfNotExist(mlir::ConversionPatternRewriter &rewriter, mlir::SymbolTableCollection &symbolTables, mlir::Operation *srcOp, llvm::StringRef fnName, mlir::Type fnTy, mlir::ArrayAttr argAttrs, mlir::ArrayAttr resAttrs)
Declare fnName with type fnTy before the function enclosing srcOp, unless the module already has a sy...
mlir::LogicalResult lowerNVPTXPrintf(cir::OffloadPrintfOp op, mlir::Value format, mlir::ValueRange args, mlir::ConversionPatternRewriter &rewriter, const mlir::DataLayout &dataLayout, mlir::SymbolTableCollection &symbolTables)
Lowers op, whose lowered format and arguments are format and args, to a call to vprintf.
__DEVICE__ _Tp arg(const std::complex< _Tp > &__c)
__builtin_bitreverse32 uint32_t __builtin_riscv_aaddu_u32 __builtin_riscv_asubu_u32 __builtin_elementwise_add_sat __builtin_elementwise_sub_sat int32_t