diff options
| author | Alex Zinenko <zinenko@google.com> | 2019-08-12 06:10:29 -0700 |
|---|---|---|
| committer | A. Unique TensorFlower <gardener@tensorflow.org> | 2019-08-12 06:10:54 -0700 |
| commit | 2dd38b09c111ff6963c5c592c76e9b771a8f691f (patch) | |
| tree | 5fa1787c102a6901b7df5d550af3ab54c2c7fb62 /mlir/lib/Target | |
| parent | 252ada493276eace3e23802fb70ff3c7be53837d (diff) | |
| download | bcm5719-llvm-2dd38b09c111ff6963c5c592c76e9b771a8f691f.tar.gz bcm5719-llvm-2dd38b09c111ff6963c5c592c76e9b771a8f691f.zip | |
LLVM dialect: introduce llvm.addressof to access globals
This instruction is a local counterpart of llvm.global that takes a symbol
reference to a global and produces an SSA value containing the pointer to it.
Used in combination, these two operations allow one to use globals with other
operations expecting SSA values. At a cost of IR indirection, we make sure the
functions don't implicitly capture the surrounding SSA values and remain
suitable for parallel processing.
PiperOrigin-RevId: 262908622
Diffstat (limited to 'mlir/lib/Target')
| -rw-r--r-- | mlir/lib/Target/LLVMIR/ModuleTranslation.cpp | 32 |
1 files changed, 23 insertions, 9 deletions
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp index 5e1109bbdd0..7a84eaea0a1 100644 --- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp @@ -247,6 +247,18 @@ LogicalResult ModuleTranslation::convertOperation(Operation &opInst, return success(); } + // Emit addressof. We need to look up the global value referenced by the + // operation and store it in the MLIR-to-LLVM value mapping. This does not + // emit any LLVM instruction. + if (auto addressOfOp = dyn_cast<LLVM::AddressOfOp>(opInst)) { + LLVM::GlobalOp global = addressOfOp.getGlobal(); + // The verifier should not have allowed this. + assert(global && "referencing an undefined global"); + + valueMapping[addressOfOp.getResult()] = globalsMapping.lookup(global); + return success(); + } + return opInst.emitError("unsupported or non-LLVM operation: ") << opInst.getName(); } @@ -290,21 +302,23 @@ LogicalResult ModuleTranslation::convertBlock(Block &bb, bool ignoreArguments) { // Create named global variables that correspond to llvm.global definitions. void ModuleTranslation::convertGlobals() { for (auto op : mlirModule.getOps<LLVM::GlobalOp>()) { + llvm::Constant *cst; + llvm::Type *type; // String attributes are treated separately because they cannot appear as // in-function constants and are thus not supported by getLLVMConstant. if (auto strAttr = op.value().dyn_cast<StringAttr>()) { - llvm::Constant *cst = llvm::ConstantDataArray::getString( + cst = llvm::ConstantDataArray::getString( llvmModule->getContext(), strAttr.getValue(), /*AddNull=*/false); - new llvm::GlobalVariable(*llvmModule, cst->getType(), op.constant(), - llvm::GlobalValue::InternalLinkage, cst, - op.sym_name()); - return; + type = cst->getType(); + } else { + type = op.getType().getUnderlyingType(); + cst = getLLVMConstant(type, op.value(), op.getLoc()); } - llvm::Type *type = op.getType().getUnderlyingType(); - new llvm::GlobalVariable( - *llvmModule, type, op.constant(), llvm::GlobalValue::InternalLinkage, - getLLVMConstant(type, op.value(), op.getLoc()), op.sym_name()); + auto *var = new llvm::GlobalVariable(*llvmModule, type, op.constant(), + llvm::GlobalValue::InternalLinkage, + cst, op.sym_name()); + globalsMapping.try_emplace(op, var); } } |

