MLIR  19.0.0git
PrintCallHelper.cpp
Go to the documentation of this file.
1 //===- PrintCallHelper.cpp - Helper to emit runtime print calls -----------===//
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 
13 #include "mlir/IR/Builders.h"
14 #include "mlir/IR/BuiltinOps.h"
15 #include "llvm/ADT/ArrayRef.h"
16 
17 using namespace mlir;
18 using namespace llvm;
19 
20 static std::string ensureSymbolNameIsUnique(ModuleOp moduleOp,
21  StringRef symbolName) {
22  static int counter = 0;
23  std::string uniqueName = std::string(symbolName);
24  while (moduleOp.lookupSymbol(uniqueName)) {
25  uniqueName = std::string(symbolName) + "_" + std::to_string(counter++);
26  }
27  return uniqueName;
28 }
29 
31  OpBuilder &builder, Location loc, ModuleOp moduleOp, StringRef symbolName,
32  StringRef string, const LLVMTypeConverter &typeConverter, bool addNewline,
33  std::optional<StringRef> runtimeFunctionName) {
34  auto ip = builder.saveInsertionPoint();
35  builder.setInsertionPointToStart(moduleOp.getBody());
36  MLIRContext *ctx = builder.getContext();
37 
38  // Create a zero-terminated byte representation and allocate global symbol.
39  SmallVector<uint8_t> elementVals;
40  elementVals.append(string.begin(), string.end());
41  if (addNewline)
42  elementVals.push_back('\n');
43  elementVals.push_back('\0');
44  auto dataAttrType = RankedTensorType::get(
45  {static_cast<int64_t>(elementVals.size())}, builder.getI8Type());
46  auto dataAttr =
47  DenseElementsAttr::get(dataAttrType, llvm::ArrayRef(elementVals));
48  auto arrayTy =
49  LLVM::LLVMArrayType::get(IntegerType::get(ctx, 8), elementVals.size());
50  auto globalOp = builder.create<LLVM::GlobalOp>(
51  loc, arrayTy, /*constant=*/true, LLVM::Linkage::Private,
52  ensureSymbolNameIsUnique(moduleOp, symbolName), dataAttr);
53 
54  auto ptrTy = LLVM::LLVMPointerType::get(builder.getContext());
55  // Emit call to `printStr` in runtime library.
56  builder.restoreInsertionPoint(ip);
57  auto msgAddr =
58  builder.create<LLVM::AddressOfOp>(loc, ptrTy, globalOp.getName());
59  SmallVector<LLVM::GEPArg> indices(1, 0);
60  Value gep =
61  builder.create<LLVM::GEPOp>(loc, ptrTy, arrayTy, msgAddr, indices);
62  Operation *printer =
63  LLVM::lookupOrCreatePrintStringFn(moduleOp, runtimeFunctionName);
64  builder.create<LLVM::CallOp>(loc, TypeRange(), SymbolRefAttr::get(printer),
65  gep);
66 }
static std::string ensureSymbolNameIsUnique(ModuleOp moduleOp, StringRef symbolName)
MLIRContext * getContext() const
Definition: Builders.h:55
IntegerType getI8Type()
Definition: Builders.cpp:79
static DenseElementsAttr get(ShapedType type, ArrayRef< Attribute > values)
Constructs a dense elements attribute from an array of element values.
Conversion from types to the LLVM IR dialect.
Definition: TypeConverter.h:34
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
This class helps build Operations.
Definition: Builders.h:209
InsertPoint saveInsertionPoint() const
Return a saved insertion point.
Definition: Builders.h:387
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
Definition: Builders.h:433
void restoreInsertionPoint(InsertPoint ip)
Restore the insert point to a previously saved point.
Definition: Builders.h:392
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:464
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:36
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
Include the generated interface declarations.
Definition: CallGraph.h:229
LLVM::LLVMFuncOp lookupOrCreatePrintStringFn(ModuleOp moduleOp, std::optional< StringRef > runtimeFunctionName={})
Declares a function to print a C-string.
void createPrintStrCall(OpBuilder &builder, Location loc, ModuleOp moduleOp, StringRef symbolName, StringRef string, const LLVMTypeConverter &typeConverter, bool addNewline=true, std::optional< StringRef > runtimeFunctionName={})
Generate IR that prints the given string to stdout.
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...