9#ifndef MLIR_LIB_TARGET_IRDLTOCPP_TEMPLATINGUTILS_H
10#define MLIR_LIB_TARGET_IRDLTOCPP_TEMPLATINGUTILS_H
12#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/StringMap.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/Support/ErrorHandling.h"
16#include "llvm/Support/raw_ostream.h"
34 bool processingReplacementToken =
false;
35 while (!str.empty()) {
36 auto [token, remainder] = str.split(
"__");
38 if (processingReplacementToken) {
39 assert(!token.empty() &&
"replacement name cannot be empty");
40 bytecode.emplace_back(ReplacementToken{token});
43 bytecode.emplace_back(LiteralToken{token});
46 processingReplacementToken = !processingReplacementToken;
54 for (
auto instruction : bytecode) {
55 if (
auto *inst = std::get_if<LiteralToken>(&instruction)) {
60 if (
auto *inst = std::get_if<ReplacementToken>(&instruction)) {
61 auto replacement = replacements.find(inst->keyName);
64 llvm::errs() <<
"Missing template key: " << inst->keyName <<
"\n";
65 llvm_unreachable(
"Missing template key");
72 llvm_unreachable(
"non-exhaustive bytecode visit");
81 struct ReplacementToken {
82 llvm::StringRef keyName;
85 std::vector<std::variant<LiteralToken, ReplacementToken>>
bytecode;
*if copies could not be generated due to yet unimplemented cases *copyInPlacementStart and copyOutPlacementStart in copyPlacementBlock *specify the insertion points where the incoming copies and outgoing should be the output argument nBegin is set to its * replacement(set to `begin` if no invalidation happens). Since outgoing *copies could have been inserted at `end`
void render(llvm::raw_ostream &out, const dictionary &replacements) const
Render will apply a dictionary to the Template and send the rendered result to the specified output s...
Template(llvm::StringRef str)
llvm::StringMap< llvm::SmallString< 8 > > dictionary
A dictionary stores a mapping of template variable names to their assigned string values.