| 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 | |
| 9 | #include "mlir/Conversion/LLVMCommon/PrintCallHelper.h" |
| 10 | #include "mlir/Conversion/LLVMCommon/TypeConverter.h" |
| 11 | #include "mlir/Dialect/LLVMIR/FunctionCallUtils.h" |
| 12 | #include "mlir/Dialect/LLVMIR/LLVMCallOps.h" |
| 13 | #include "mlir/Dialect/LLVMIR/LLVMConstantOps.h" |
| 14 | #include "mlir/Dialect/LLVMIR/LLVMMemoryOps.h" |
| 15 | #include "mlir/Dialect/LLVMIR/LLVMModuleOps.h" |
| 16 | #include "mlir/IR/Builders.h" |
| 17 | #include "mlir/IR/BuiltinOps.h" |
| 18 | #include "llvm/ADT/ArrayRef.h" |
| 19 | |
| 20 | using namespace mlir; |
| 21 | using namespace llvm; |
| 22 | |
| 23 | /// Check if a given symbol name is already in use within the module operation. |
| 24 | /// If no symbol with such name is present, then the same identifier is |
| 25 | /// returned. Otherwise, a unique and yet unused identifier is computed starting |
| 26 | /// from the requested one. |
| 27 | static std::string |
| 28 | ensureSymbolNameIsUnique(ModuleOp moduleOp, StringRef symbolName, |
| 29 | SymbolTableCollection *symbolTables = nullptr) { |
| 30 | if (symbolTables) { |
| 31 | SymbolTable &symbolTable = symbolTables->getSymbolTable(op: moduleOp); |
| 32 | unsigned counter = 0; |
| 33 | SmallString<128> uniqueName = symbolTable.generateSymbolName<128>( |
| 34 | name: symbolName, |
| 35 | uniqueChecker: [&](const SmallString<128> &tentativeName) { |
| 36 | return symbolTable.lookupSymbolIn(op: moduleOp, symbol: tentativeName) != nullptr; |
| 37 | }, |
| 38 | uniquingCounter&: counter); |
| 39 | |
| 40 | return static_cast<std::string>(uniqueName); |
| 41 | } |
| 42 | |
| 43 | static int counter = 0; |
| 44 | std::string uniqueName = std::string(symbolName); |
| 45 | while (moduleOp.lookupSymbol(name: uniqueName)) { |
| 46 | uniqueName = std::string(symbolName) + "_" + std::to_string(val: counter++); |
| 47 | } |
| 48 | return uniqueName; |
| 49 | } |
| 50 | |
| 51 | LogicalResult mlir::LLVM::createPrintStrCall( |
| 52 | OpBuilder &builder, Location loc, ModuleOp moduleOp, StringRef symbolName, |
| 53 | StringRef string, const LLVMTypeConverter &typeConverter, bool addNewline, |
| 54 | std::optional<StringRef> runtimeFunctionName, |
| 55 | SymbolTableCollection *symbolTables) { |
| 56 | auto ip = builder.saveInsertionPoint(); |
| 57 | builder.setInsertionPointToStart(moduleOp.getBody()); |
| 58 | MLIRContext *ctx = builder.getContext(); |
| 59 | |
| 60 | // Create a zero-terminated byte representation and allocate global symbol. |
| 61 | SmallVector<uint8_t> elementVals; |
| 62 | elementVals.append(in_start: string.begin(), in_end: string.end()); |
| 63 | if (addNewline) |
| 64 | elementVals.push_back(Elt: '\n'); |
| 65 | elementVals.push_back(Elt: '\0'); |
| 66 | auto dataAttrType = RankedTensorType::get( |
| 67 | shape: {static_cast<int64_t>(elementVals.size())}, elementType: builder.getI8Type()); |
| 68 | auto dataAttr = |
| 69 | DenseElementsAttr::get(type: dataAttrType, values: llvm::ArrayRef(elementVals)); |
| 70 | auto arrayTy = |
| 71 | LLVM::LLVMArrayType::get(elementType: IntegerType::get(context: ctx, width: 8), numElements: elementVals.size()); |
| 72 | auto globalOp = LLVM::GlobalOp::create( |
| 73 | builder, location: loc, type: arrayTy, /*isConstant=*/true, linkage: LLVM::Linkage::Private, |
| 74 | name: ensureSymbolNameIsUnique(moduleOp, symbolName, symbolTables), value: dataAttr); |
| 75 | |
| 76 | auto ptrTy = LLVM::LLVMPointerType::get(context: builder.getContext()); |
| 77 | // Emit call to `printStr` in runtime library. |
| 78 | builder.restoreInsertionPoint(ip); |
| 79 | auto msgAddr = |
| 80 | LLVM::AddressOfOp::create(builder, location: loc, res: ptrTy, global_name: globalOp.getName()); |
| 81 | SmallVector<LLVM::GEPArg> indices(1, 0); |
| 82 | Value gep = |
| 83 | LLVM::GEPOp::create(builder, location: loc, resultType: ptrTy, elementType: arrayTy, basePtr: msgAddr, indices); |
| 84 | FailureOr<LLVM::LLVMFuncOp> printer = |
| 85 | LLVM::lookupOrCreatePrintStringFn(b&: builder, moduleOp, runtimeFunctionName); |
| 86 | if (failed(Result: printer)) |
| 87 | return failure(); |
| 88 | LLVM::CallOp::create(builder, location: loc, results: TypeRange(), |
| 89 | callee: SymbolRefAttr::get(symbol: printer.value()), args: gep); |
| 90 | return success(); |
| 91 | } |
| 92 | |