| 1 | //===- LowerToLLVMOffloadPrintfNVPTX.cpp - NVPTX printf lowering ----------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file lowers cir.offload.printf to vprintf, the printf runtime interface |
| 10 | // of NVPTX. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "LowerToLLVM.h" |
| 15 | #include "LowerToLLVMOffloadPrintf.h" |
| 16 | |
| 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" |
| 22 | #include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 23 | |
| 24 | namespace cir { |
| 25 | namespace direct { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | // vprintf takes two args: A format string, and a pointer to a buffer containing |
| 30 | // the varargs. |
| 31 | // |
| 32 | // For example, the call |
| 33 | // |
| 34 | // printf("format string", arg1, arg2, arg3); |
| 35 | // |
| 36 | // is converted into something resembling |
| 37 | // |
| 38 | // struct Tmp { |
| 39 | // Arg1 a1; |
| 40 | // Arg2 a2; |
| 41 | // Arg3 a3; |
| 42 | // }; |
| 43 | // char* buf = alloca(sizeof(Tmp)); |
| 44 | // *(Tmp*)buf = {a1, a2, a3}; |
| 45 | // vprintf("format string", buf); |
| 46 | // |
| 47 | // `buf` is aligned to the max of {alignof(Arg1), ...}. Furthermore, each of |
| 48 | // the args is itself aligned to its ABI alignment. |
| 49 | // |
| 50 | // Note that by the time this function runs, the arguments have already |
| 51 | // undergone the standard C vararg promotion (short -> int, float -> double |
| 52 | // etc). In this function we pack the arguments into the buffer described above. |
| 53 | mlir::Value |
| 54 | packArgsIntoNVPTXFormatBuffer(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(context: rewriter.getContext()); |
| 59 | |
| 60 | if (args.empty()) |
| 61 | // If there are no arguments other than the format string, |
| 62 | // pass a nullptr to vprintf. |
| 63 | return mlir::LLVM::ZeroOp::create(builder&: rewriter, location: loc, res: ptrTy); |
| 64 | |
| 65 | // We can directly store the arguments into a struct, and the alignment |
| 66 | // would automatically be correct. That's because vprintf does not |
| 67 | // accept aggregates. |
| 68 | auto allocaTy = mlir::LLVM::LLVMStructType::getLiteral( |
| 69 | context: rewriter.getContext(), types: llvm::to_vector(Range: args.getTypes())); |
| 70 | |
| 71 | // Allocate the buffer in the entry block, so that a printf in a loop does |
| 72 | // not grow the stack on every iteration. |
| 73 | mlir::Value alloca; |
| 74 | { |
| 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()); |
| 79 | mlir::Value one = |
| 80 | mlir::LLVM::ConstantOp::create(builder&: rewriter, location: loc, type: rewriter.getI32Type(), value: 1); |
| 81 | alloca = |
| 82 | mlir::LLVM::AllocaOp::create(builder&: rewriter, location: loc, resultType: ptrTy, elementType: allocaTy, arraySize: one, |
| 83 | alignment: dataLayout.getTypeABIAlignment(t: allocaTy)); |
| 84 | } |
| 85 | |
| 86 | // Member accesses are inbounds and nuw, as for cir.get_member. |
| 87 | mlir::LLVM::GEPNoWrapFlags flags = |
| 88 | mlir::LLVM::GEPNoWrapFlags::inbounds | mlir::LLVM::GEPNoWrapFlags::nuw; |
| 89 | for (auto [i, arg] : llvm::enumerate(First&: args)) { |
| 90 | mlir::Value member = mlir::LLVM::GEPOp::create( |
| 91 | builder&: rewriter, location: loc, resultType: ptrTy, elementType: allocaTy, basePtr: alloca, |
| 92 | indices: llvm::ArrayRef<mlir::LLVM::GEPArg>{0, static_cast<int32_t>(i)}, noWrapFlags: flags); |
| 93 | mlir::LLVM::StoreOp::create(builder&: rewriter, location: loc, value: arg, addr: member, |
| 94 | alignment: dataLayout.getTypeABIAlignment(t: arg.getType())); |
| 95 | } |
| 96 | |
| 97 | return alloca; |
| 98 | } |
| 99 | |
| 100 | } // namespace |
| 101 | |
| 102 | // Lowers a printf to a call to vprintf, as GPUPrintfOpToVPrintfLowering does |
| 103 | // for gpu.printf. |
| 104 | mlir::LogicalResult |
| 105 | lowerNVPTXPrintf(cir::OffloadPrintfOp op, mlir::Value format, |
| 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); |
| 112 | |
| 113 | // int vprintf(char *format, void *packedData); |
| 114 | auto ptrTy = mlir::LLVM::LLVMPointerType::get(context: rewriter.getContext()); |
| 115 | mlir::Type i32Ty = rewriter.getI32Type(); |
| 116 | const llvm::StringRef fnName = "vprintf" ; |
| 117 | createLLVMFuncOpIfNotExist( |
| 118 | rewriter, symbolTables, srcOp: op, fnName, |
| 119 | fnTy: mlir::LLVM::LLVMFunctionType::get(result: i32Ty, arguments: {ptrTy, ptrTy})); |
| 120 | // vprintf takes a generic pointer, which can address a format string in any |
| 121 | // address space. |
| 122 | if (format.getType() != ptrTy) |
| 123 | format = mlir::LLVM::AddrSpaceCastOp::create(builder&: rewriter, location: op.getLoc(), res: ptrTy, |
| 124 | arg: format); |
| 125 | rewriter.replaceOpWithNewOp<mlir::LLVM::CallOp>( |
| 126 | op, args: mlir::TypeRange{i32Ty}, args: fnName, args: mlir::ValueRange{format, packedData}); |
| 127 | return mlir::success(); |
| 128 | } |
| 129 | |
| 130 | } // namespace direct |
| 131 | } // namespace cir |
| 132 | |