To support the `-fvisibility=...` option in Flang, we need a pass to rewrite all the global definitions in the LLVM dialect that have the default visibility to have the specified visibility. This change adds such a pass. Note that I did not add an option for `visiblity=default`; I believe this makes sense for compiler drivers since users may want to tack an option on at the end of a compile line to override earlier options, but I don't think it makes sense for this pass to accept `visibility=default`--it would just be an early exit IIUC.
55 lines
1.8 KiB
C++
55 lines
1.8 KiB
C++
//===- UseDefaultVisibilityPass.cpp - Update default visibility -----------===//
|
|
//
|
|
// 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/UseDefaultVisibilityPass.h"
|
|
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
|
|
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
|
|
#include "mlir/Pass/Pass.h"
|
|
|
|
namespace mlir {
|
|
namespace LLVM {
|
|
#define GEN_PASS_DEF_LLVMUSEDEFAULTVISIBILITYPASS
|
|
#include "mlir/Dialect/LLVMIR/Transforms/Passes.h.inc"
|
|
} // namespace LLVM
|
|
} // namespace mlir
|
|
|
|
using namespace mlir;
|
|
|
|
static void updateVisibility(Operation *op,
|
|
LLVM::VisibilityAttr newVisibilityAttr) {
|
|
if (auto visibilityAttr =
|
|
op->getAttrOfType<LLVM::VisibilityAttr>(LLVM::VisibilityAttr::name)) {
|
|
LLVM::Visibility visibility = visibilityAttr.getValue();
|
|
if (visibility == LLVM::Visibility::Default) {
|
|
op->setAttr(LLVM::VisibilityAttr::name, newVisibilityAttr);
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
class UseDefaultVisibilityPass
|
|
: public LLVM::impl::LLVMUseDefaultVisibilityPassBase<
|
|
UseDefaultVisibilityPass> {
|
|
using Base::Base;
|
|
|
|
public:
|
|
void runOnOperation() override {
|
|
LLVM::Visibility useDefaultVisibility = useVisibility.getValue();
|
|
Operation *op = getOperation();
|
|
MLIRContext *context = op->getContext();
|
|
Dialect *llvmDialect = context->getLoadedDialect<LLVM::LLVMDialect>();
|
|
auto newVisibilityAttr =
|
|
LLVM::VisibilityAttr::get(context, useDefaultVisibility);
|
|
op->walk([&](Operation *op) {
|
|
if (op->getDialect() == llvmDialect)
|
|
updateVisibility(op, newVisibilityAttr);
|
|
});
|
|
}
|
|
};
|
|
} // namespace
|