[llvm] IR2Builder tool for converting LLVM IR files into C++ IRBuilder API (PR #117129)

Jason Eckhardt via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 21 18:44:22 PST 2024


================
@@ -0,0 +1,1702 @@
+//===-- ir2builder.cpp - Transpiler from IR to builder API ------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines a pass, which converts LLVM IR into llvm::IRBuilder
+// API in textual form.
+//
+// This tool can be used to simplify IR construction using IRBuilder by
+// writing the IR by hand and then converting it using this pass.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/APFloat.h"
+#include "llvm/ADT/APInt.h"
+#include "llvm/ADT/PostOrderIterator.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/AsmParser/Parser.h"
+#include "llvm/IR/CallingConv.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/InlineAsm.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/ModuleSummaryIndex.h"
+#include "llvm/Support/AtomicOrdering.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/InitLLVM.h"
+#include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/ToolOutputFile.h"
+#include "llvm/Support/raw_ostream.h"
+#include <algorithm>
+#include <cctype>
+#include <cmath>
+#include <cstdint>
+#include <sstream>
+
+using namespace llvm;
+
+cl::OptionCategory Ir2BCat("ir2builder Options");
+
+/** LLVM IR to convert */
+static cl::opt<std::string>
+    InputFilename(cl::Positional, cl::desc("<input .ll file>"), cl::init("-"));
+
+/**
+ * Output file for the generated C++ code.
+ * If not set then stdout will be used.
+ */
+static cl::opt<std::string> OutputFilename("o",
+                                           cl::desc("Override output filename"),
+                                           cl::value_desc("filename"),
+                                           cl::cat(Ir2BCat));
+
+/** Set this when you don't use `using namespace llvm` (don't forget :: at the
+ * end) */
+static cl::opt<std::string> ScopePrefix(
+    "scope-prefix",
+    cl::desc(
+        "All generated calls to LLVM API will be prefixed with this scope. The "
+        "scope has to end with '::' (e.g.: '-scope-prefix=llvm::')"),
+    cl::cat(Ir2BCat), cl::init(""));
+
+/** Set this to your variable name for IRBuilder instance */
+static cl::opt<std::string> BuilderName(
+    "builder-name",
+    cl::desc("IRBuilder variable name that will be used in generated code"),
+    cl::cat(Ir2BCat), cl::init("Builder"));
+
+/** Set this to your LLVMContext variable name */
+static cl::opt<std::string> ContextName(
+    "context-name",
+    cl::desc("Context variable name that will be used in generated code"),
+    cl::cat(Ir2BCat), cl::init("Ctx"));
+
+/** Set this to your llvm::Module * name */
+static cl::opt<std::string> ModuleName(
+    "module-name",
+    cl::desc("Module variable name that will be used in generated code"),
+    cl::cat(Ir2BCat), cl::init("Mod"));
+
+/** Set this if you want custom data layout */
+static cl::opt<std::string> ClDataLayout("data-layout",
+                                         cl::desc("data layout string to use"),
+                                         cl::value_desc("layout-string"),
+                                         cl::init(""), cl::cat(Ir2BCat));
+
+/**
+ * This will generate fully compilable C++ program with main, which will
+ * use generated calls to create the original LLVM IR.
+ * Main purpose of this is to use it for testing and verification that
+ * generated program is correct and compilable.
+ * For example you can generate the code, run this program and then use
+ * llvm-diff to see if it matches the original:
+ *     ```
+ *     ./ir2builder code.ll --runnable > main.cpp &&
+ *     g++ main.cpp `./llvm-conf --cxxflags --ldflags --system-libs --libs core`
+ * -I /llvm/include/ -o main &&
+ *     ./main > generated.ll &&
+ *     ./llvm-diff code.ll generated.ll
+ *     ```
+ */
+static cl::opt<bool> GenerateRunnable(
+    "runnable", cl::desc("Generates whole cpp compilable program with main"),
+    cl::init(false), cl::cat(Ir2BCat));
+
+/**
+ * Disables verification of loaded llvm IR.
+ * Keep in mind that this will most likely result in C++ error as it probably
+ * won't be possible to create Builder calls for this.
+ */
+static cl::opt<bool>
+    DisableVerify("disable-verify", cl::Hidden,
+                  cl::desc("Do not run verifier on input LLVM (dangerous!)"),
+                  cl::cat(Ir2BCat));
+
+/**
+ * Sets the order of traversal for the LLVM IR.
+ * When enabled the traversal will be in reverse post order, which can handle
+ * when values are defined after (text-wise) their use.
+ * On the other hand using just linear traversal will also include parts that
+ * are outside of the graph (dead blocks).
+ */
+static cl::opt<bool>
+    UseRPO("use-rpo",
+           cl::desc("Traverses IR in reverse post order. This can help with "
+                    "\"was not declared\" errors"),
+           cl::init(true), cl::cat(Ir2BCat));
+
+/**
+ * @brief Transpiler from LLVM IR into IRBuilder API calls
+ * The main purpose for this class is to hold variable counter and variable
+ * names for needed resources, such as LLVMContext.
+ */
+class IR2Builder {
+private:
+  unsigned long varI = 0;
+  std::string llvmPrefix;
+  std::string builder;
+  std::string ctx;
+  std::string modName;
+
+  std::vector<std::string> phiIncomings;
+
+  inline bool hasName(const Value *op) {
+    return !isa<Constant>(op) && !isa<InlineAsm>(op);
+  }
+
+  void outputAttr(Attribute att, raw_ostream &OS);
+
+  std::string getNextVar();
+  std::string getLinkage(GlobalValue &gv);
+  std::string getThreadLocalMode(GlobalValue::ThreadLocalMode tlm);
+  std::string getCmpPredicate(CmpInst::Predicate p);
+  std::string getAtomicRMWOp(AtomicRMWInst::BinOp op);
+  std::string getAtomicOrdering(AtomicOrdering ao);
+  std::string getSyncScopeID(SyncScope::ID sys);
+  std::string getCallingConv(CallingConv::ID cc);
+  std::string getConstantRange(ConstantRange &cr);
+
+  std::string getVal(const Value *op);
+  std::string getConst(const Constant *op);
+  std::string getType(const Type *t);
+  std::string getInlineAsm(const InlineAsm *op);
+  std::string getMetadata(const Metadata *op);
+
+public:
+  IR2Builder()
+      : llvmPrefix(ScopePrefix), builder(BuilderName), ctx(ContextName),
+        modName(ModuleName) {}
+
+  /**
+   * Calls convert for all the functions in passed in module
+   * @param M Module to call convert over
+   * @param OS Stream to which output the builder calls
+   */
+  void convert(Module &M, raw_ostream &OS);
+
+  /**
+   * Converts a function into IRBuilder API calls
+   * @param F Function to convert
+   * @param OS Stream to which output the builder calls
+   */
+  void convert(Function &F, raw_ostream &OS);
+
+  /**
+   * Converts an instruction into IRBuilder API calls
+   * @param I Instruction to convert
+   * @param OS Stream to which output the builder calls
+   * @note Unsupported instructions or their operands should result
+   *       in a TODO comment.
+   */
+  void convert(const Instruction *I, raw_ostream &OS);
+};
+
+std::string IR2Builder::getNextVar() {
+  return "v0" + std::to_string(varI++);
+}
+
+static std::string to_str(bool b) { return b ? "true" : "false"; }
+
+static std::string escape(std::string str) {
+  std::stringstream ss;
+  for (unsigned char C : str) {
+    if (C == '\\')
+      ss << '\\' << C;
+    else if (isPrint(C) && C != '"')
+      ss << C;
+    else
+      ss << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
+  }
+  return ss.str();
+}
+
+static std::string sanitize(std::string s) {
+  std::stringstream ss;
+  for (size_t i = 0; i < s.size(); ++i) {
+    if (!std::isalnum(s[i])) {
+      ss << "_" << static_cast<unsigned>(s[i]) << "_";
+    } else {
+      ss << s[i];
+    }
+  }
+  return ss.str();
+}
+
+std::string IR2Builder::getLinkage(llvm::GlobalValue &gv) {
+  std::string link = llvmPrefix + "GlobalValue::";
+
+  if (gv.hasExternalLinkage()) {
+    return link + "ExternalLinkage";
+  }
+  if (gv.hasAvailableExternallyLinkage()) {
+    return link + "AvailableExternallyLinkage";
+  }
+  if (gv.hasLinkOnceAnyLinkage()) {
+    return link + "LinkOnceAnyLinkage";
+  }
+  if (gv.hasLinkOnceODRLinkage()) {
+    return link + "LinkOnceODRLinkage";
+  }
+  if (gv.hasWeakAnyLinkage()) {
+    return link + "WeakAnyLinkage";
+  }
+  if (gv.hasWeakODRLinkage()) {
+    return link + "WeakODRLinkage";
+  }
+  if (gv.hasAppendingLinkage()) {
+    return link + "AppendingLinkage";
+  }
+  if (gv.hasInternalLinkage()) {
+    return link + "InternalLinkage";
+  }
+  if (gv.hasPrivateLinkage()) {
+    return link + "PrivateLinkage";
+  }
+  if (gv.hasExternalWeakLinkage()) {
+    return link + "ExternalWeakLinkage";
+  }
+  if (gv.hasCommonLinkage()) {
+    return link + "CommonLinkage";
+  }
+
+  return "/* Unknown linkage */";
+}
+
+std::string IR2Builder::getAtomicRMWOp(AtomicRMWInst::BinOp op) {
+  switch (op) {
+  case AtomicRMWInst::BinOp::Xchg:
+    return llvmPrefix + "AtomicRMWInst::BinOp::Xchg";
+  case AtomicRMWInst::BinOp::Add:
+    return llvmPrefix + "AtomicRMWInst::BinOp::Add";
+  case AtomicRMWInst::BinOp::Sub:
+    return llvmPrefix + "AtomicRMWInst::BinOp::Sub";
+  case AtomicRMWInst::BinOp::And:
+    return llvmPrefix + "AtomicRMWInst::BinOp::And";
+  case AtomicRMWInst::BinOp::Nand:
+    return llvmPrefix + "AtomicRMWInst::BinOp::Nand";
+  case AtomicRMWInst::BinOp::Or:
+    return llvmPrefix + "AtomicRMWInst::BinOp::Or";
+  case AtomicRMWInst::BinOp::Xor:
+    return llvmPrefix + "AtomicRMWInst::BinOp::Xor";
+  case AtomicRMWInst::BinOp::Max:
+    return llvmPrefix + "AtomicRMWInst::BinOp::Max";
+  case AtomicRMWInst::BinOp::Min:
+    return llvmPrefix + "AtomicRMWInst::BinOp::Min";
+  case AtomicRMWInst::BinOp::UMax:
+    return llvmPrefix + "AtomicRMWInst::BinOp::UMax";
+  case AtomicRMWInst::BinOp::UMin:
+    return llvmPrefix + "AtomicRMWInst::BinOp::UMin";
+  case AtomicRMWInst::BinOp::FAdd:
+    return llvmPrefix + "AtomicRMWInst::BinOp::FAdd";
+  case AtomicRMWInst::BinOp::FSub:
+    return llvmPrefix + "AtomicRMWInst::BinOp::FSub";
+  case AtomicRMWInst::BinOp::FMax:
+    return llvmPrefix + "AtomicRMWInst::BinOp::FMax";
+  case AtomicRMWInst::BinOp::FMin:
+    return llvmPrefix + "AtomicRMWInst::BinOp::FMin";
+  case AtomicRMWInst::BinOp::UIncWrap:
+    return llvmPrefix + "AtomicRMWInst::BinOp::UIncWrap";
+  case AtomicRMWInst::BinOp::UDecWrap:
+    return llvmPrefix + "AtomicRMWInst::BinOp::UDecWrap";
+  default:
+    return "/* TODO: Unknown AtomicRMW operator (using value) */ " +
+           std::to_string(op);
+  }
+}
+
+std::string IR2Builder::getAtomicOrdering(AtomicOrdering ao) {
+  switch (ao) {
+  case AtomicOrdering::NotAtomic:
+    return llvmPrefix + "AtomicOrdering::NotAtomic";
+  case AtomicOrdering::Unordered:
+    return llvmPrefix + "AtomicOrdering::Unordered";
+  case AtomicOrdering::Monotonic:
+    return llvmPrefix + "AtomicOrdering::Monotonic";
+  case AtomicOrdering::Acquire:
+    return llvmPrefix + "AtomicOrdering::Acquire";
+  case AtomicOrdering::Release:
+    return llvmPrefix + "AtomicOrdering::Release";
+  case AtomicOrdering::AcquireRelease:
+    return llvmPrefix + "AtomicOrdering::AcquireRelease";
+  case AtomicOrdering::SequentiallyConsistent:
+    return llvmPrefix + "AtomicOrdering::SequentiallyConsistent";
+  default:
+    return "/* TODO: Unknown atomic ordering */";
+  }
+}
+
+std::string IR2Builder::getCmpPredicate(CmpInst::Predicate p) {
+  switch (p) {
+  case CmpInst::Predicate::FCMP_FALSE:
+    return llvmPrefix + "CmpInst::Predicate::FCMP_FALSE";
+  case CmpInst::Predicate::FCMP_OEQ:
+    return llvmPrefix + "CmpInst::Predicate::FCMP_OEQ";
+  case CmpInst::Predicate::FCMP_OGT:
+    return llvmPrefix + "CmpInst::Predicate::FCMP_OGT";
+  case CmpInst::Predicate::FCMP_OGE:
+    return llvmPrefix + "CmpInst::Predicate::FCMP_OGE";
+  case CmpInst::Predicate::FCMP_OLT:
+    return llvmPrefix + "CmpInst::Predicate::FCMP_OLT";
+  case CmpInst::Predicate::FCMP_OLE:
+    return llvmPrefix + "CmpInst::Predicate::FCMP_OLE";
+  case CmpInst::Predicate::FCMP_ONE:
+    return llvmPrefix + "CmpInst::Predicate::FCMP_ONE";
+  case CmpInst::Predicate::FCMP_ORD:
+    return llvmPrefix + "CmpInst::Predicate::FCMP_ORD";
+  case CmpInst::Predicate::FCMP_UNO:
+    return llvmPrefix + "CmpInst::Predicate::FCMP_UNO";
+  case CmpInst::Predicate::FCMP_UEQ:
+    return llvmPrefix + "CmpInst::Predicate::FCMP_UEQ";
+  case CmpInst::Predicate::FCMP_UGT:
+    return llvmPrefix + "CmpInst::Predicate::FCMP_UGT";
+  case CmpInst::Predicate::FCMP_UGE:
+    return llvmPrefix + "CmpInst::Predicate::FCMP_UGE";
+  case CmpInst::Predicate::FCMP_ULT:
+    return llvmPrefix + "CmpInst::Predicate::FCMP_ULT";
+  case CmpInst::Predicate::FCMP_ULE:
+    return llvmPrefix + "CmpInst::Predicate::FCMP_ULE";
+  case CmpInst::Predicate::FCMP_UNE:
+    return llvmPrefix + "CmpInst::Predicate::FCMP_UNE";
+  case CmpInst::Predicate::FCMP_TRUE:
+    return llvmPrefix + "CmpInst::Predicate::FCMP_TRUE";
+  case CmpInst::Predicate::ICMP_EQ:
+    return llvmPrefix + "CmpInst::Predicate::ICMP_EQ";
+  case CmpInst::Predicate::ICMP_NE:
+    return llvmPrefix + "CmpInst::Predicate::ICMP_NE";
+  case CmpInst::Predicate::ICMP_UGT:
+    return llvmPrefix + "CmpInst::Predicate::ICMP_UGT";
+  case CmpInst::Predicate::ICMP_UGE:
+    return llvmPrefix + "CmpInst::Predicate::ICMP_UGE";
+  case CmpInst::Predicate::ICMP_ULT:
+    return llvmPrefix + "CmpInst::Predicate::ICMP_ULT";
+  case CmpInst::Predicate::ICMP_ULE:
+    return llvmPrefix + "CmpInst::Predicate::ICMP_ULE";
+  case CmpInst::Predicate::ICMP_SGT:
+    return llvmPrefix + "CmpInst::Predicate::ICMP_SGT";
+  case CmpInst::Predicate::ICMP_SGE:
+    return llvmPrefix + "CmpInst::Predicate::ICMP_SGE";
+  case CmpInst::Predicate::ICMP_SLT:
+    return llvmPrefix + "CmpInst::Predicate::ICMP_SLT";
+  case CmpInst::Predicate::ICMP_SLE:
+    return llvmPrefix + "CmpInst::Predicate::ICMP_SLE";
+  default:
+    return "/* TODO: Unknown CMP predicate (using value) */ " +
+           std::to_string(p);
+  }
+}
+
+std::string IR2Builder::getSyncScopeID(SyncScope::ID sys) {
+  if (sys == SyncScope::System)
+    return llvmPrefix + "SyncScope::System";
+  else if (sys == SyncScope::SingleThread)
+    return llvmPrefix + "SyncScope::SingleThread";
+  else
+    return "/* TODO: Unknown SyncScope ID (using value) */ " +
+           std::to_string(sys);
+}
+
+std::string IR2Builder::getThreadLocalMode(GlobalValue::ThreadLocalMode tlm) {
+  switch (tlm) {
+  case GlobalValue::ThreadLocalMode::NotThreadLocal:
+    return llvmPrefix + "GlobalValue::ThreadLocalMode::NotThreadLocal";
+  case GlobalValue::ThreadLocalMode::GeneralDynamicTLSModel:
+    return llvmPrefix + "GlobalValue::ThreadLocalMode::GeneralDynamicTLSModel";
+  case GlobalValue::ThreadLocalMode::LocalDynamicTLSModel:
+    return llvmPrefix + "GlobalValue::ThreadLocalMode::LocalDynamicTLSModel";
+  case GlobalValue::ThreadLocalMode::InitialExecTLSModel:
+    return llvmPrefix + "GlobalValue::ThreadLocalMode::InitialExecTLSModel";
+  case GlobalValue::ThreadLocalMode::LocalExecTLSModel:
+    return llvmPrefix + "GlobalValue::ThreadLocalMode::LocalExecTLSModel";
+  default:
+    return "/* TODO: Unknown ThreadLocalMode (using value) */ " +
+           std::to_string(tlm);
+  }
+}
+
+std::string IR2Builder::getCallingConv(CallingConv::ID cc) {
+  switch (cc) {
+  case CallingConv::C:
+    return llvmPrefix + "CallingConv::C";
+  case CallingConv::Fast:
+    return llvmPrefix + "CallingConv::Fast";
+  case CallingConv::Cold:
+    return llvmPrefix + "CallingConv::Cold";
+  case CallingConv::GHC:
+    return llvmPrefix + "CallingConv::GHC";
+  case CallingConv::HiPE:
+    return llvmPrefix + "CallingConv::HiPE";
+  case CallingConv::AnyReg:
+    return llvmPrefix + "CallingConv::AnyReg";
+  case CallingConv::PreserveMost:
+    return llvmPrefix + "CallingConv::PreserveMost";
+  case CallingConv::PreserveAll:
+    return llvmPrefix + "CallingConv::PreserveAll";
+  case CallingConv::Swift:
+    return llvmPrefix + "CallingConv::Swift";
+  case CallingConv::CXX_FAST_TLS:
+    return llvmPrefix + "CallingConv::CXX_FAST_TLS";
+  case CallingConv::Tail:
+    return llvmPrefix + "CallingConv::Tail";
+  case CallingConv::CFGuard_Check:
+    return llvmPrefix + "CallingConv::CFGuard_Check";
+  case CallingConv::SwiftTail:
+    return llvmPrefix + "CallingConv::SwiftTail";
+  case CallingConv::PreserveNone:
+    return llvmPrefix + "CallingConv::PreserveNone";
+  case CallingConv::FirstTargetCC:
+    return llvmPrefix + "CallingConv::FirstTargetCC";
+  // CallingConv::X86_StdCall is the same as FirstTargetCC
+  case CallingConv::X86_FastCall:
+    return llvmPrefix + "CallingConv::X86_FastCall";
+  case CallingConv::ARM_APCS:
+    return llvmPrefix + "CallingConv::ARM_APCS";
+  case CallingConv::ARM_AAPCS:
+    return llvmPrefix + "CallingConv::ARM_AAPCS";
+  case CallingConv::ARM_AAPCS_VFP:
+    return llvmPrefix + "CallingConv::ARM_AAPCS_VFP";
+  case CallingConv::MSP430_INTR:
+    return llvmPrefix + "CallingConv::MSP430_INTR";
+  case CallingConv::X86_ThisCall:
+    return llvmPrefix + "CallingConv::X86_ThisCall";
+  case CallingConv::PTX_Kernel:
+    return llvmPrefix + "CallingConv::PTX_Kernel";
+  case CallingConv::PTX_Device:
+    return llvmPrefix + "CallingConv::PTX_Device";
+  case CallingConv::SPIR_FUNC:
+    return llvmPrefix + "CallingConv::SPIR_FUNC";
+  case CallingConv::SPIR_KERNEL:
+    return llvmPrefix + "CallingConv::SPIR_KERNEL";
+  case CallingConv::Intel_OCL_BI:
+    return llvmPrefix + "CallingConv::Intel_OCL_BI";
+  case CallingConv::X86_64_SysV:
+    return llvmPrefix + "CallingConv::X86_64_SysV";
+  case CallingConv::Win64:
+    return llvmPrefix + "CallingConv::Win64";
+  case CallingConv::X86_VectorCall:
+    return llvmPrefix + "CallingConv::X86_VectorCall";
+  case CallingConv::DUMMY_HHVM:
+    return llvmPrefix + "CallingConv::DUMMY_HHVM";
+  case CallingConv::DUMMY_HHVM_C:
+    return llvmPrefix + "CallingConv::DUMMY_HHVM_C";
+  case CallingConv::X86_INTR:
+    return llvmPrefix + "CallingConv::X86_INTR";
+  case CallingConv::AVR_INTR:
+    return llvmPrefix + "CallingConv::AVR_INTR";
+  case CallingConv::AVR_SIGNAL:
+    return llvmPrefix + "CallingConv::AVR_SIGNAL";
+  case CallingConv::AVR_BUILTIN:
+    return llvmPrefix + "CallingConv::AVR_BUILTIN";
+  case CallingConv::AMDGPU_VS:
+    return llvmPrefix + "CallingConv::AMDGPU_VS";
+  case CallingConv::AMDGPU_GS:
+    return llvmPrefix + "CallingConv::AMDGPU_GS";
+  case CallingConv::AMDGPU_PS:
+    return llvmPrefix + "CallingConv::AMDGPU_PS";
+  case CallingConv::AMDGPU_CS:
+    return llvmPrefix + "CallingConv::AMDGPU_CS";
+  case CallingConv::AMDGPU_KERNEL:
+    return llvmPrefix + "CallingConv::AMDGPU_KERNEL";
+  case CallingConv::X86_RegCall:
+    return llvmPrefix + "CallingConv::X86_RegCall";
+  case CallingConv::AMDGPU_HS:
+    return llvmPrefix + "CallingConv::AMDGPU_HS";
+  case CallingConv::MSP430_BUILTIN:
+    return llvmPrefix + "CallingConv::MSP430_BUILTIN";
+  case CallingConv::AMDGPU_LS:
+    return llvmPrefix + "CallingConv::AMDGPU_LS";
+  case CallingConv::AMDGPU_ES:
+    return llvmPrefix + "CallingConv::AMDGPU_ES";
+  case CallingConv::AArch64_VectorCall:
+    return llvmPrefix + "CallingConv::AArch64_VectorCall";
+  case CallingConv::AArch64_SVE_VectorCall:
+    return llvmPrefix + "CallingConv::AArch64_SVE_VectorCall";
+  case CallingConv::WASM_EmscriptenInvoke:
+    return llvmPrefix + "CallingConv::WASM_EmscriptenInvoke";
+  case CallingConv::AMDGPU_Gfx:
+    return llvmPrefix + "CallingConv::AMDGPU_Gfx";
+  case CallingConv::M68k_INTR:
+    return llvmPrefix + "CallingConv::M68k_INTR";
+  case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0:
+    return llvmPrefix +
+           "CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0";
+  case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2:
+    return llvmPrefix +
+           "CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2";
+  case CallingConv::AMDGPU_CS_Chain:
+    return llvmPrefix + "CallingConv::AMDGPU_CS_Chain";
+  case CallingConv::AMDGPU_CS_ChainPreserve:
+    return llvmPrefix + "CallingConv::AMDGPU_CS_ChainPreserve";
+  case CallingConv::M68k_RTD:
+    return llvmPrefix + "CallingConv::M68k_RTD";
+  case CallingConv::GRAAL:
+    return llvmPrefix + "CallingConv::GRAAL";
+  case CallingConv::ARM64EC_Thunk_X64:
+    return llvmPrefix + "CallingConv::ARM64EC_Thunk_X64";
+  case CallingConv::ARM64EC_Thunk_Native:
+    return llvmPrefix + "CallingConv::ARM64EC_Thunk_Native";
+  default:
+    return "/* Custom CC */" + std::to_string(cc);
+  }
+}
+
+std::string IR2Builder::getType(const Type *t) {
+  std::string tcall;
+  if (auto tt = dyn_cast<IntegerType>(t)) {
+    switch (tt->getBitWidth()) {
+    case 8:
+      tcall = "getInt8Ty()";
+      break;
+    case 16:
+      tcall = "getInt16Ty()";
+      break;
+    case 32:
+      tcall = "getInt32Ty()";
+      break;
+    case 64:
+      tcall = "getInt64Ty()";
+      break;
+    default:
+      tcall = "getIntNTy(" + std::to_string(tt->getBitWidth()) + ")";
+      break;
----------------
nvjle wrote:

We can dedup four specific cases as:
```
...
     case 8:
     case 16:
     case 32:
     case 64:
      tcall = ("getInt" + Twine(tt->getBitWidth()) + "Ty()").str();
      break;
...

```

https://github.com/llvm/llvm-project/pull/117129


More information about the llvm-commits mailing list