Andrew Rogers ffc4d87f9b
[llvm] annotate interfaces in Passes for DLL export (#143794)
## Purpose

This patch is one in a series of code-mods that annotate LLVM’s public
interface for export. This patch annotates the `llvm/Passes` library and
other pass-related headers. These annotations currently have no
meaningful impact on the LLVM build; however, they are a prerequisite to
support an LLVM Windows DLL (shared library) build.

## Background

This effort is tracked in #109483. Additional context is provided in
[this
discourse](https://discourse.llvm.org/t/psa-annotating-llvm-public-interface/85307),
and documentation for `LLVM_ABI` and related annotations is found in the
LLVM repo
[here](https://github.com/llvm/llvm-project/blob/main/llvm/docs/InterfaceExportAnnotations.rst).

The bulk of these changes were generated automatically using the
[Interface Definition Scanner (IDS)](https://github.com/compnerd/ids)
tool, followed formatting with `git clang-format`.

The following manual adjustments were also applied after running IDS on
Linux:
- Remove the redundant declaration of the `initializeKCFIPass` function
from llvm/include/llvm/InitializePasses.h because IDS only
auto-annotates the first declaration it encounters, and the second
un-annotated declaration results in an MSVC warning
- Add `LLVM_ABI` to a number of private `AnalysisKey` fields in classes
that extend the `AnalysisInfoMixin` template class.
- Add `LLVM_ABI` to the `ChangeReporter` and `TextChangeReporter`
template class definitions in
llvm/include/llvm/Passes/StandardInstrumentations.h and remove the
extern template instantiations. This is the only way I've found to get
everything compiling warning-free when building a DLL because both
template classes have methods implemented out-of-line.

## Validation

Local builds and tests to validate cross-platform compatibility. This
included llvm, clang, and lldb on the following configurations:

- Windows with MSVC
- Windows with Clang
- Linux with GCC
- Linux with Clang
- Darwin with Clang
2025-06-16 11:03:48 -07:00

126 lines
5.3 KiB
C++

//===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file defines stuff that is used to define and "use" Passes. This file
// is automatically #included by Pass.h, so:
//
// NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
//
// Instead, #include Pass.h.
//
// This file defines Pass registration code and classes used for it.
//
//===----------------------------------------------------------------------===//
#if !defined(LLVM_PASS_H) || defined(LLVM_PASSSUPPORT_H)
#error "Do not include <PassSupport.h>; include <Pass.h> instead"
#endif
#ifndef LLVM_PASSSUPPORT_H
#define LLVM_PASSSUPPORT_H
#include "llvm/ADT/StringRef.h"
#include "llvm/PassInfo.h"
#include "llvm/PassRegistry.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/Threading.h"
#include <functional>
namespace llvm {
class Pass;
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
static void initialize##passName##PassOnce(PassRegistry &Registry) {
#define INITIALIZE_PASS_DEPENDENCY(depName) initialize##depName##Pass(Registry);
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \
PassInfo *PI = new PassInfo( \
name, arg, &passName::ID, \
PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis); \
Registry.registerPass(*PI, true); \
} \
static llvm::once_flag Initialize##passName##PassFlag; \
void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
llvm::call_once(Initialize##passName##PassFlag, \
initialize##passName##PassOnce, std::ref(Registry)); \
}
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \
INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_WITH_OPTIONS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
PassName::registerOptions();
#define INITIALIZE_PASS_WITH_OPTIONS(PassName, Arg, Name, Cfg, Analysis) \
INITIALIZE_PASS_WITH_OPTIONS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
INITIALIZE_PASS_END(PassName, Arg, Name, Cfg, Analysis)
template <class PassName> Pass *callDefaultCtor() {
if constexpr (std::is_default_constructible_v<PassName>)
return new PassName();
else
// Some codegen passes should only be testable via
// `llc -{start|stop}-{before|after}=<passname>`, not via `opt -<passname>`.
report_fatal_error("target-specific codegen-only pass");
}
//===---------------------------------------------------------------------------
/// RegisterPass<t> template - This template class is used to notify the system
/// that a Pass is available for use, and registers it into the internal
/// database maintained by the PassManager. Unless this template is used, opt,
/// for example will not be able to see the pass and attempts to create the pass
/// will fail. This template is used in the follow manner (at global scope, in
/// your .cpp file):
///
/// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
///
/// This statement will cause your pass to be created by calling the default
/// constructor exposed by the pass.
template <typename passName> struct RegisterPass : public PassInfo {
// Register Pass using default constructor...
RegisterPass(StringRef PassArg, StringRef Name, bool CFGOnly = false,
bool is_analysis = false)
: PassInfo(Name, PassArg, &passName::ID,
PassInfo::NormalCtor_t(callDefaultCtor<passName>), CFGOnly,
is_analysis) {
PassRegistry::getPassRegistry()->registerPass(*this);
}
};
//===---------------------------------------------------------------------------
/// PassRegistrationListener class - This class is meant to be derived from by
/// clients that are interested in which passes get registered and unregistered
/// at runtime (which can be because of the RegisterPass constructors being run
/// as the program starts up, or may be because a shared object just got
/// loaded).
struct PassRegistrationListener {
PassRegistrationListener() = default;
virtual ~PassRegistrationListener() = default;
/// Callback functions - These functions are invoked whenever a pass is loaded
/// or removed from the current executable.
virtual void passRegistered(const PassInfo *) {}
/// enumeratePasses - Iterate over the registered passes, calling the
/// passEnumerate callback on each PassInfo object.
LLVM_ABI void enumeratePasses();
/// passEnumerate - Callback function invoked when someone calls
/// enumeratePasses on this PassRegistrationListener object.
virtual void passEnumerate(const PassInfo *) {}
};
} // end namespace llvm
#endif // LLVM_PASSSUPPORT_H