[Mlir-commits] [mlir] [mlir] Add pass to add comdat to all linkonce functions (PR #65270)

Mehdi Amini llvmlistbot at llvm.org
Wed Sep 6 15:30:01 PDT 2023


================
@@ -0,0 +1,78 @@
+//===- AddComdats.cpp - Prepare for translation to LLVM IR ----------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/LLVMIR/Transforms/AddComdats.h"
+#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
+#include "mlir/IR/PatternMatch.h"
+#include "mlir/Pass/Pass.h"
+#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
+
+namespace mlir {
+namespace LLVM {
+#define GEN_PASS_DEF_LLVMADDCOMDATS
+#include "mlir/Dialect/LLVMIR/Transforms/Passes.h.inc"
+} // namespace LLVM
+} // namespace mlir
+
+using namespace mlir;
+
+static void addComdat(LLVM::LLVMFuncOp &op, PatternRewriter &rewriter,
+                      ModuleOp &module) {
+  const char *comdatName = "__llvm_comdat";
+  mlir::LLVM::ComdatOp comdatOp =
+      module.lookupSymbol<mlir::LLVM::ComdatOp>(comdatName);
+  if (!comdatOp) {
+    PatternRewriter::InsertionGuard guard(rewriter);
+    rewriter.setInsertionPointToStart(module.getBody());
+    comdatOp =
+        rewriter.create<mlir::LLVM::ComdatOp>(module.getLoc(), comdatName);
+  }
+
+  PatternRewriter::InsertionGuard guard(rewriter);
+  rewriter.setInsertionPointToStart(&comdatOp.getBody().back());
+  auto selectorOp = rewriter.create<mlir::LLVM::ComdatSelectorOp>(
+      comdatOp.getLoc(), op.getSymName(), mlir::LLVM::comdat::Comdat::Any);
+  rewriter.updateRootInPlace(op, [&]() {
+    op.setComdatAttr(mlir::SymbolRefAttr::get(
+        rewriter.getContext(), comdatName,
+        mlir::FlatSymbolRefAttr::get(selectorOp.getSymNameAttr())));
+  });
+}
+
+struct AddComdat : public OpRewritePattern<LLVM::LLVMFuncOp> {
+  using OpRewritePattern<LLVM::LLVMFuncOp>::OpRewritePattern;
+
+private:
+  LogicalResult matchAndRewrite(LLVM::LLVMFuncOp op,
+                                PatternRewriter &rewriter) const override {
+    if (op.getComdat() || (op.getLinkage() != LLVM::Linkage::Linkonce &&
+                           op.getLinkage() != LLVM::Linkage::LinkonceODR))
+      return failure();
+
+    auto mod = op->getParentOfType<mlir::ModuleOp>();
+    addComdat(op, rewriter, mod);
+    return success();
+  }
+};
+
+namespace {
+struct AddComdatsPass : public LLVM::impl::LLVMAddComdatsBase<AddComdatsPass> {
+  void runOnOperation() override {
+    MLIRContext *ctx = &getContext();
+    RewritePatternSet patterns(ctx);
+    patterns.add<AddComdat>(ctx);
+    if (failed(
+            applyPatternsAndFoldGreedily(getOperation(), std::move(patterns))))
----------------
joker-eph wrote:

Using patterns is far too expensive and overkill for what is a simple traversal here I think.

Seems like this Pass should:
1) run on a SymbolOpInterface (most of the time the module).
2) Build a symbol table once and for all.
3) Iterated on the immediately nested operation and do a single traversal.



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


More information about the Mlir-commits mailing list