Commit c33d6970 authored by River Riddle's avatar River Riddle
Browse files

[mlir] Add support for basic location translation to LLVM.

Summary:
This revision adds basic support for emitting line table information when exporting to LLVMIR. We don't yet have a story for supporting all of the LLVM debug metadata, so this revision stubs some features(like subprograms) to enable emitting line tables.

Differential Revision: https://reviews.llvm.org/D73934
parent c3f0ed7b
Loading
Loading
Loading
Loading
+22 −18
Original line number Diff line number Diff line
@@ -32,13 +32,17 @@ class Operation;

namespace LLVM {

namespace detail {
class DebugTranslation;
} // end namespace detail

class LLVMFuncOp;

// Implementation class for module translation.  Holds a reference to the module
// being translated, and the mappings between the original and the translated
// functions, basic blocks and values.  It is practically easier to hold these
// mappings in one class since the conversion of control flow operations
// needs to look up block and function mappings.
/// Implementation class for module translation. Holds a reference to the module
/// being translated, and the mappings between the original and the translated
/// functions, basic blocks and values. It is practically easier to hold these
/// mappings in one class since the conversion of control flow operations
/// needs to look up block and function mappings.
class ModuleTranslation {
public:
  template <typename T = ModuleTranslation>
@@ -51,8 +55,7 @@ public:
    if (!llvmModule)
      return nullptr;

    T translator(m);
    translator.llvmModule = std::move(llvmModule);
    T translator(m, std::move(llvmModule));
    translator.convertGlobals();
    if (failed(translator.convertFunctions()))
      return nullptr;
@@ -65,14 +68,12 @@ public:
  static Block &getModuleBody(Operation *m) { return m->getRegion(0).front(); }

protected:
  // Translate the given MLIR module expressed in MLIR LLVM IR dialect into an
  // LLVM IR module.  The MLIR LLVM IR dialect holds a pointer to an
  // LLVMContext, the LLVM IR module will be created in that context.
  explicit ModuleTranslation(Operation *module) : mlirModule(module) {
    assert(satisfiesLLVMModule(mlirModule) &&
           "mlirModule should honor LLVM's module semantics.");
  }
  virtual ~ModuleTranslation() {}
  /// Translate the given MLIR module expressed in MLIR LLVM IR dialect into an
  /// LLVM IR module. The MLIR LLVM IR dialect holds a pointer to an
  /// LLVMContext, the LLVM IR module will be created in that context.
  ModuleTranslation(Operation *module,
                    std::unique_ptr<llvm::Module> llvmModule);
  virtual ~ModuleTranslation();

  virtual LogicalResult convertOperation(Operation &op,
                                         llvm::IRBuilder<> &builder);
@@ -94,15 +95,18 @@ private:
  llvm::Constant *getLLVMConstant(llvm::Type *llvmType, Attribute attr,
                                  Location loc);

  // Original and translated module.
  /// Original and translated module.
  Operation *mlirModule;
  std::unique_ptr<llvm::Module> llvmModule;

  // Mappings between llvm.mlir.global definitions and corresponding globals.
  /// A converter for translating debug information.
  std::unique_ptr<detail::DebugTranslation> debugTranslation;

  /// Mappings between llvm.mlir.global definitions and corresponding globals.
  DenseMap<Operation *, llvm::GlobalValue *> globalsMapping;

protected:
  // Mappings between original and translated values, used for lookups.
  /// Mappings between original and translated values, used for lookups.
  llvm::StringMap<llvm::Function *> functionMapping;
  DenseMap<Value, llvm::Value *> valueMapping;
  DenseMap<Block *, llvm::BasicBlock *> blockMapping;
+1 −1
Original line number Diff line number Diff line
@@ -118,7 +118,7 @@ std::unique_ptr<OpPassBase<FuncOp>> createAffineDataCopyGenerationPass(
std::unique_ptr<OpPassBase<FuncOp>> createMemRefDataFlowOptPass();

/// Creates a pass to strip debug information from a function.
std::unique_ptr<OpPassBase<FuncOp>> createStripDebugInfoPass();
std::unique_ptr<Pass> createStripDebugInfoPass();

/// Creates a pass which tests loop fusion utilities.
std::unique_ptr<OpPassBase<FuncOp>> createTestLoopFusionPass();
+1 −0
Original line number Diff line number Diff line
add_llvm_library(MLIRTargetLLVMIRModuleTranslation
  LLVMIR/DebugTranslation.cpp
  LLVMIR/ModuleTranslation.cpp

  ADDITIONAL_HEADER_DIRS
+1 −5
Original line number Diff line number Diff line
@@ -48,11 +48,8 @@ static llvm::Intrinsic::ID getShflBflyIntrinsicId(llvm::Type *resultType,

namespace {
class ModuleTranslation : public LLVM::ModuleTranslation {

public:
  explicit ModuleTranslation(Operation *module)
      : LLVM::ModuleTranslation(module) {}
  ~ModuleTranslation() override {}
  using LLVM::ModuleTranslation::ModuleTranslation;

protected:
  LogicalResult convertOperation(Operation &opInst,
@@ -66,7 +63,6 @@ protected:
} // namespace

std::unique_ptr<llvm::Module> mlir::translateModuleToNVVMIR(Operation *m) {
  ModuleTranslation translation(m);
  auto llvmModule =
      LLVM::ModuleTranslation::translateModule<ModuleTranslation>(m);
  if (!llvmModule)
+1 −6
Original line number Diff line number Diff line
@@ -57,11 +57,8 @@ static llvm::Value *createDeviceFunctionCall(llvm::IRBuilder<> &builder,

namespace {
class ModuleTranslation : public LLVM::ModuleTranslation {

public:
  explicit ModuleTranslation(Operation *module)
      : LLVM::ModuleTranslation(module) {}
  ~ModuleTranslation() override {}
  using LLVM::ModuleTranslation::ModuleTranslation;

protected:
  LogicalResult convertOperation(Operation &opInst,
@@ -75,8 +72,6 @@ protected:
} // namespace

std::unique_ptr<llvm::Module> mlir::translateModuleToROCDLIR(Operation *m) {
  ModuleTranslation translation(m);

  // lower MLIR (with RODL Dialect) to LLVM IR (with ROCDL intrinsics)
  auto llvmModule =
      LLVM::ModuleTranslation::translateModule<ModuleTranslation>(m);
Loading