17#include "mlir/Dialect/LLVMIR/LLVMCallOps.h"
18#include "mlir/Dialect/LLVMIR/LLVMConstantOps.h"
19#include "mlir/Dialect/LLVMIR/LLVMConversionOps.h"
20#include "mlir/Dialect/LLVMIR/LLVMMemoryOps.h"
21#include "mlir/Dialect/LLVMIR/LLVMModuleOps.h"
54packArgsIntoNVPTXFormatBuffer(cir::OffloadPrintfOp op, mlir::ValueRange args,
55 mlir::ConversionPatternRewriter &rewriter,
56 const mlir::DataLayout &dataLayout) {
57 mlir::Location loc = op.getLoc();
58 auto ptrTy = mlir::LLVM::LLVMPointerType::get(rewriter.getContext());
63 return mlir::LLVM::ZeroOp::create(rewriter, loc, ptrTy);
68 auto allocaTy = mlir::LLVM::LLVMStructType::getLiteral(
69 rewriter.getContext(), llvm::to_vector(args.getTypes()));
75 mlir::OpBuilder::InsertionGuard guard(rewriter);
76 auto fn = op->getParentOfType<mlir::LLVM::LLVMFuncOp>();
77 assert(fn &&
"lowering expects printf to be inside a function");
78 rewriter.setInsertionPointToStart(&fn.getBody().front());
80 mlir::LLVM::ConstantOp::create(rewriter, loc, rewriter.getI32Type(), 1);
82 mlir::LLVM::AllocaOp::create(rewriter, loc, ptrTy, allocaTy, one,
83 dataLayout.getTypeABIAlignment(allocaTy));
87 mlir::LLVM::GEPNoWrapFlags flags =
88 mlir::LLVM::GEPNoWrapFlags::inbounds | mlir::LLVM::GEPNoWrapFlags::nuw;
89 for (
auto [i, arg] : llvm::enumerate(args)) {
90 mlir::Value
member = mlir::LLVM::GEPOp::create(
91 rewriter, loc, ptrTy, allocaTy, alloca,
92 llvm::ArrayRef<mlir::LLVM::GEPArg>{0,
static_cast<int32_t>(i)}, flags);
93 mlir::LLVM::StoreOp::create(rewriter, loc, arg, member,
94 dataLayout.getTypeABIAlignment(
arg.getType()));
106 mlir::ValueRange args,
107 mlir::ConversionPatternRewriter &rewriter,
108 const mlir::DataLayout &dataLayout,
109 mlir::SymbolTableCollection &symbolTables) {
110 mlir::Value packedData =
111 packArgsIntoNVPTXFormatBuffer(op, args, rewriter, dataLayout);
114 auto ptrTy = mlir::LLVM::LLVMPointerType::get(rewriter.getContext());
115 mlir::Type i32Ty = rewriter.getI32Type();
116 const llvm::StringRef fnName =
"vprintf";
118 rewriter, symbolTables, op, fnName,
119 mlir::LLVM::LLVMFunctionType::get(i32Ty, {ptrTy, ptrTy}));
122 if (format.getType() != ptrTy)
123 format = mlir::LLVM::AddrSpaceCastOp::create(rewriter, op.getLoc(), ptrTy,
125 rewriter.replaceOpWithNewOp<mlir::LLVM::CallOp>(
126 op, mlir::TypeRange{i32Ty}, fnName, mlir::ValueRange{format, packedData});
127 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_elementwise_add_sat __builtin_elementwise_sub_sat int32_t