clang 24.0.0git
LowerToLLVMOffloadPrintfNVPTX.cpp
Go to the documentation of this file.
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"
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"
23
24namespace cir {
25namespace direct {
26
27namespace {
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.
53mlir::Value
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());
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(rewriter, loc, 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 rewriter.getContext(), llvm::to_vector(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(rewriter, loc, rewriter.getI32Type(), 1);
81 alloca =
82 mlir::LLVM::AllocaOp::create(rewriter, loc, ptrTy, allocaTy, one,
83 dataLayout.getTypeABIAlignment(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(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()));
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.
104mlir::LogicalResult
105lowerNVPTXPrintf(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(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}));
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(rewriter, op.getLoc(), ptrTy,
124 format);
125 rewriter.replaceOpWithNewOp<mlir::LLVM::CallOp>(
126 op, mlir::TypeRange{i32Ty}, fnName, mlir::ValueRange{format, packedData});
127 return mlir::success();
128}
129
130} // namespace direct
131} // namespace cir
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.
RangeSelector member(std::string ID)
Given a MemberExpr, selects the member token. ID is the node's binding in the match result.
__DEVICE__ _Tp arg(const std::complex< _Tp > &__c)
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat int32_t