MLIR  22.0.0git
AddComdats.cpp
Go to the documentation of this file.
1 //===- AddComdats.cpp - Add comdats to linkonce functions -----------------===//
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 
11 #include "mlir/Pass/Pass.h"
12 
13 namespace mlir {
14 namespace LLVM {
15 #define GEN_PASS_DEF_LLVMADDCOMDATS
16 #include "mlir/Dialect/LLVMIR/Transforms/Passes.h.inc"
17 } // namespace LLVM
18 } // namespace mlir
19 
20 using namespace mlir;
21 
22 static void addComdat(LLVM::LLVMFuncOp &op, OpBuilder &builder,
23  SymbolTable &symbolTable, ModuleOp &module) {
24  const char *comdatName = "__llvm_comdat";
25  mlir::LLVM::ComdatOp comdatOp =
26  symbolTable.lookup<mlir::LLVM::ComdatOp>(comdatName);
27  if (!comdatOp) {
28  PatternRewriter::InsertionGuard guard(builder);
29  builder.setInsertionPointToStart(module.getBody());
30  comdatOp =
31  mlir::LLVM::ComdatOp::create(builder, module.getLoc(), comdatName);
32  symbolTable.insert(comdatOp);
33  }
34 
35  PatternRewriter::InsertionGuard guard(builder);
36  builder.setInsertionPointToStart(&comdatOp.getBody().back());
37  auto selectorOp = mlir::LLVM::ComdatSelectorOp::create(
38  builder, comdatOp.getLoc(), op.getSymName(),
39  mlir::LLVM::comdat::Comdat::Any);
40  op.setComdatAttr(mlir::SymbolRefAttr::get(
41  builder.getContext(), comdatName,
42  mlir::FlatSymbolRefAttr::get(selectorOp.getSymNameAttr())));
43 }
44 
45 namespace {
46 struct AddComdatsPass : public LLVM::impl::LLVMAddComdatsBase<AddComdatsPass> {
47  void runOnOperation() override {
48  OpBuilder builder{&getContext()};
49  ModuleOp mod = getOperation();
50 
51  std::unique_ptr<SymbolTable> symbolTable;
52  auto getSymTab = [&]() -> SymbolTable & {
53  if (!symbolTable)
54  symbolTable = std::make_unique<SymbolTable>(mod);
55  return *symbolTable;
56  };
57  for (auto op : mod.getBody()->getOps<LLVM::LLVMFuncOp>()) {
58  if (op.getLinkage() == LLVM::Linkage::Linkonce ||
59  op.getLinkage() == LLVM::Linkage::LinkonceODR) {
60  addComdat(op, builder, getSymTab(), mod);
61  }
62  }
63  }
64 };
65 } // namespace
static void addComdat(LLVM::LLVMFuncOp &op, OpBuilder &builder, SymbolTable &symbolTable, ModuleOp &module)
Definition: AddComdats.cpp:22
static MLIRContext * getContext(OpFoldResult val)
MLIRContext * getContext() const
Definition: Builders.h:55
static FlatSymbolRefAttr get(StringAttr value)
Construct a symbol reference for the given value name.
This class helps build Operations.
Definition: Builders.h:205
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
Definition: Builders.h:429
This class allows for representing and managing the symbol table used by operations with the 'SymbolT...
Definition: SymbolTable.h:24
Operation * lookup(StringRef name) const
Look up a symbol with the specified name, returning null if no such name exists.
StringAttr insert(Operation *symbol, Block::iterator insertPt={})
Insert a new symbol into the table, and rename it as necessary to avoid collisions.
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...