Skip to content

[Support] Add clang tooling generated explicit visibility macros #113097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions llvm/include/llvm/Support/AMDGPUMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define LLVM_SUPPORT_AMDGPUMETADATA_H

#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Compiler.h"
#include <cstdint>
#include <string>
#include <system_error>
Expand Down Expand Up @@ -447,10 +448,10 @@ struct Metadata final {
};

/// Converts \p String to \p HSAMetadata.
std::error_code fromString(StringRef String, Metadata &HSAMetadata);
LLVM_ABI std::error_code fromString(StringRef String, Metadata &HSAMetadata);

/// Converts \p HSAMetadata to \p String.
std::error_code toString(Metadata HSAMetadata, std::string &String);
LLVM_ABI std::error_code toString(Metadata HSAMetadata, std::string &String);

//===----------------------------------------------------------------------===//
// HSA metadata for v3 code object.
Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/Support/ARMAttributeParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
#include "ARMBuildAttributes.h"
#include "ELFAttributeParser.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Error.h"

namespace llvm {

class ScopedPrinter;

class ARMAttributeParser : public ELFAttributeParser {
class LLVM_ABI ARMAttributeParser : public ELFAttributeParser {
struct DisplayHandler {
ARMBuildAttrs::AttrType attribute;
Error (ARMAttributeParser::*routine)(ARMBuildAttrs::AttrType);
Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/Support/ARMBuildAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
#ifndef LLVM_SUPPORT_ARMBUILDATTRIBUTES_H
#define LLVM_SUPPORT_ARMBUILDATTRIBUTES_H

#include "llvm/Support/Compiler.h"
#include "llvm/Support/ELFAttributes.h"

namespace llvm {
namespace ARMBuildAttrs {

const TagNameMap &getARMAttributeTags();
LLVM_ABI const TagNameMap &getARMAttributeTags();

enum SpecialAttr {
// This is for the .cpu asm attr. It translates into one or more
Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/Support/ARMWinEH.h
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional that the classes in this file are not marked LLVM_ABI?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any class with no out of line members is skipped to help reduce the number of exports created for MSVC.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, do you have any idea what fraction of classes are like that? This seems like a bit of a foot-gun, as someone adding an out of line member is likely not going to check whether this is the first out of line member in the class and they have to add an ABI annotation.

Or is it expected that automation is going to check that all necessary annotations are present and we instead have to explicitly mark things that we don't want part of the ABI (with which macro? It is still the LIBRARY_VISIBILITY one?)

Generally I'd appreciate it if the PR included some useful context on what the wider maintenance plan here is supposed to be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the idea is automate adding new annotations by running something like post commit action like gn syncbot does. There some discussion about this in the Clang PR #109702.

It is still the LIBRARY_VISIBILITY one?

I just added a new macro LLVM_ABI_NOT_EXPORTED that the tool looks for, I never really though about reusing LIBRARY_VISIBILITY maybe i could. It would have to be not defined when the tool runs so the tool can define it as a clang annotate attribute it can find.
One of the extra goals is default symbol visibility is eventually set to hidden for non windows platforms to also reduce the number of exported symbols for them #19707.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of this is also discussed in @compnerds old thread https://discourse.llvm.org/t/supporting-llvm-build-llvm-dylib-on-windows/58891

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added a new macro LLVM_ABI_NOT_EXPORTED that the tool looks for, I never really though about reusing LIBRARY_VISIBILITY maybe i could. It would have to be not defined when the tool runs so the tool can define it as a clang annotate attribute it can find.

It would generally be nice if the old LLVM_EXTERNAL_VISIBILTY, LLVM_LIBRARY_VISIBILITY and LLVM_ALWAYS_EXPORT and the new macros could be consolidated. Having 3 old macros on top of 5 new ones really doesn't make it easy to understand when you're supposed to use which one.

And on that note, it would be great to have a section in https://llvm.org/docs/ProgrammersManual.html that explains the different ABI macros and when you're supposed to use them. As these are going to be become widespread now, having a reference that we can point people to will be helpful. (There is some limited documentation in Compiler.h, but it does lose nuances like the "only use if there is an out of line member" guideline mentioned above.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would generally be nice if the old LLVM_EXTERNAL_VISIBILTY, LLVM_LIBRARY_VISIBILITY and LLVM_ALWAYS_EXPORT and the new macros could be consolidated. Having 3 old macros on top of 5 new ones really doesn't make it easy to understand when you're supposed to use which one.

This has been my concern as well. However, this is currently an intermediatory step. The desired end goal is to have ~3-4 of these macros for the libraries that we want to support.

In my mind the ones that we want:

  • LLVM_SUPPORT_ABI - ABI surface for LLVMSupport
  • LLVM_ABI - ABI surface for LLVM (DSO)
  • LLVM_C_ABI - ABI surface for llvm-c
  • CLANG_ABI - ABI surface for clang (DSO)
  • LLDB_ABI - ABI surface for LLDB (DSO)

This would allow most of the common uses of the library builds be possible on all the platforms. There is a need for splitting out LLVMSupport as it is so commonly used, and LLVM and LLVM C are separate as there is somewhat of an ABI guarantee on the LLVM C interface.

And on that note, it would be great to have a section in https://llvm.org/docs/ProgrammersManual.html that explains the different ABI macros and when you're supposed to use them. As these are going to be become widespread now, having a reference that we can point people to will be helpful. (There is some limited documentation in Compiler.h, but it does lose nuances like the "only use if there is an out of line member" guideline mentioned above.)

I agree that documenting them is important. However, my concern is that it is going to spread fear and confusion due to the current state. We need to get to the state where the macros are properly setup so that we are able to build the DLLs with the reduced macro set to demonstrate that we are in the right state. At that point, as this becomes part of the development workflow, it would be something that must be documented and have clear guidelines on how to use the tooling to verify (as well as builds).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that documenting them is important. However, my concern is that it is going to spread fear and confusion due to the current state. We need to get to the state where the macros are properly setup so that we are able to build the DLLs with the reduced macro set to demonstrate that we are in the right state. At that point, as this becomes part of the development workflow, it would be something that must be documented and have clear guidelines on how to use the tooling to verify (as well as builds).

As the discussion on this PR and the clang one show, you already have fear and confusion now :)

I'd be fine with going ahead with this as long as there is some place we can point people to explaining what is going on here and what the plan going forward is. And by "some place" I don't mean "breadcrumbs spread across five different issues, PRs and discourse threads".

There should be a discourse post that says something along the lines of: We are going to be mass adding ABI annotations (because...), you should just ignore them for now (we'll add them to any new code ourselves), we're going to add automation (which does...?) to make sure they stay up to date once the bulk has landed. The annotations will only get used once we have automation and documentation in place.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLVM_SUPPORT_ARMWINEH_H

#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Endian.h"

namespace llvm {
Expand Down Expand Up @@ -205,7 +206,7 @@ inline uint16_t StackAdjustment(const RuntimeFunction &RF) {

/// SavedRegisterMask - Utility function to calculate the set of saved general
/// purpose (r0-r15) and VFP (d0-d31) registers.
std::pair<uint16_t, uint32_t> SavedRegisterMask(const RuntimeFunction &RF,
LLVM_ABI std::pair<uint16_t, uint32_t> SavedRegisterMask(const RuntimeFunction &RF,
bool Prologue = true);

/// RuntimeFunctionARM64 - An entry in the table of procedure data (.pdata)
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Support/Allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace detail {

// We call out to an external function to actually print the message as the
// printing code uses Allocator.h in its implementation.
void printBumpPtrAllocatorStats(unsigned NumSlabs, size_t BytesAllocated,
LLVM_ABI void printBumpPtrAllocatorStats(unsigned NumSlabs, size_t BytesAllocated,
size_t TotalMemory);

} // end namespace detail
Expand Down
5 changes: 3 additions & 2 deletions llvm/include/llvm/Support/Atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef LLVM_SUPPORT_ATOMIC_H
#define LLVM_SUPPORT_ATOMIC_H

#include "llvm/Support/Compiler.h"
#include "llvm/Support/DataTypes.h"

// Windows will at times define MemoryFence.
Expand All @@ -26,14 +27,14 @@

namespace llvm {
namespace sys {
void MemoryFence();
LLVM_ABI void MemoryFence();

#ifdef _MSC_VER
typedef long cas_flag;
#else
typedef uint32_t cas_flag;
#endif
cas_flag CompareAndSwap(volatile cas_flag* ptr,
LLVM_ABI cas_flag CompareAndSwap(volatile cas_flag* ptr,
cas_flag new_value,
cas_flag old_value);
}
Expand Down
7 changes: 4 additions & 3 deletions llvm/include/llvm/Support/BalancedPartitioning.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

#include "raw_ostream.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/Compiler.h"

#include <atomic>
#include <condition_variable>
Expand All @@ -53,7 +54,7 @@ namespace llvm {
class ThreadPoolInterface;
/// A function with a set of utility nodes where it is beneficial to order two
/// functions close together if they have similar utility nodes
class BPFunctionNode {
class LLVM_ABI BPFunctionNode {
friend class BalancedPartitioning;

public:
Expand Down Expand Up @@ -97,7 +98,7 @@ struct BalancedPartitioningConfig {
unsigned TaskSplitDepth = 9;
};

class BalancedPartitioning {
class LLVM_ABI BalancedPartitioning {
public:
BalancedPartitioning(const BalancedPartitioningConfig &Config);

Expand All @@ -114,7 +115,7 @@ class BalancedPartitioning {
/// wait(). BalancedPartitioning recursively spawns new threads inside other
/// threads, so we need to track how many active threads that could spawn more
/// threads.
struct BPThreadPool {
struct LLVM_ABI BPThreadPool {
ThreadPoolInterface &TheThreadPool;
std::mutex mtx;
std::condition_variable cv;
Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/Support/Base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef LLVM_SUPPORT_BASE64_H
#define LLVM_SUPPORT_BASE64_H

#include "llvm/Support/Compiler.h"
#include "llvm/Support/Error.h"
#include <cstdint>
#include <string>
Expand Down Expand Up @@ -54,7 +55,7 @@ template <class InputBytes> std::string encodeBase64(InputBytes const &Bytes) {
return Buffer;
}

llvm::Error decodeBase64(llvm::StringRef Input, std::vector<char> &Output);
LLVM_ABI llvm::Error decodeBase64(llvm::StringRef Input, std::vector<char> &Output);

} // end namespace llvm

Expand Down
5 changes: 3 additions & 2 deletions llvm/include/llvm/Support/BinaryStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/BitmaskEnum.h"
#include "llvm/Support/BinaryStreamError.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Error.h"
#include <cstdint>

Expand All @@ -31,7 +32,7 @@ enum BinaryStreamFlags {
/// single contiguous buffer (or even in memory at all), in such cases a it may
/// be necessary for an implementation to cache such a buffer so that it can
/// return it.
class BinaryStream {
class LLVM_ABI BinaryStream {
public:
virtual ~BinaryStream() = default;

Expand Down Expand Up @@ -69,7 +70,7 @@ class BinaryStream {
/// buffer to the stream's backing store. Streams are assumed to be buffered
/// so that to be portable it is necessary to call commit() on the stream when
/// all data has been written.
class WritableBinaryStream : public BinaryStream {
class LLVM_ABI WritableBinaryStream : public BinaryStream {
public:
~WritableBinaryStream() override = default;

Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/Support/BinaryStreamError.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLVM_SUPPORT_BINARYSTREAMERROR_H

#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Error.h"

#include <string>
Expand All @@ -24,7 +25,7 @@ enum class stream_error_code {
};

/// Base class for errors originating when parsing raw PDB files
class BinaryStreamError : public ErrorInfo<BinaryStreamError> {
class LLVM_ABI BinaryStreamError : public ErrorInfo<BinaryStreamError> {
public:
static char ID;
explicit BinaryStreamError(stream_error_code C);
Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/Support/BinaryStreamReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "llvm/Support/Alignment.h"
#include "llvm/Support/BinaryStreamArray.h"
#include "llvm/Support/BinaryStreamRef.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Error.h"
Expand All @@ -26,7 +27,7 @@ namespace llvm {
/// null-terminated strings, integers in various flavors of endianness, etc.
/// Can be subclassed to provide reading of custom datatypes, although no
/// are overridable.
class BinaryStreamReader {
class LLVM_ABI BinaryStreamReader {
public:
BinaryStreamReader() = default;
explicit BinaryStreamReader(BinaryStreamRef Ref);
Expand Down
5 changes: 3 additions & 2 deletions llvm/include/llvm/Support/BinaryStreamRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/BinaryStream.h"
#include "llvm/Support/BinaryStreamError.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Error.h"
#include <cstdint>
#include <memory>
Expand Down Expand Up @@ -150,7 +151,7 @@ template <class RefType, class StreamType> class BinaryStreamRefBase {
/// general, you should not pass around pointers or references to BinaryStreams
/// and use inheritance to achieve polymorphism. Instead, you should pass
/// around BinaryStreamRefs by value and achieve polymorphism that way.
class BinaryStreamRef
class LLVM_ABI BinaryStreamRef
: public BinaryStreamRefBase<BinaryStreamRef, BinaryStream> {
friend BinaryStreamRefBase<BinaryStreamRef, BinaryStream>;
friend class WritableBinaryStreamRef;
Expand Down Expand Up @@ -214,7 +215,7 @@ struct BinarySubstreamRef {
bool empty() const { return size() == 0; }
};

class WritableBinaryStreamRef
class LLVM_ABI WritableBinaryStreamRef
: public BinaryStreamRefBase<WritableBinaryStreamRef,
WritableBinaryStream> {
friend BinaryStreamRefBase<WritableBinaryStreamRef, WritableBinaryStream>;
Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/Support/BinaryStreamWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "llvm/Support/BinaryStreamArray.h"
#include "llvm/Support/BinaryStreamError.h"
#include "llvm/Support/BinaryStreamRef.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Error.h"
#include <cstdint>
Expand All @@ -27,7 +28,7 @@ namespace llvm {
/// such as null-terminated strings, integers in various flavors of endianness,
/// etc. Can be subclassed to provide reading and writing of custom datatypes,
/// although no methods are overridable.
class BinaryStreamWriter {
class LLVM_ABI BinaryStreamWriter {
public:
BinaryStreamWriter() = default;
explicit BinaryStreamWriter(WritableBinaryStreamRef Ref);
Expand Down
5 changes: 3 additions & 2 deletions llvm/include/llvm/Support/BlockFrequency.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef LLVM_SUPPORT_BLOCKFREQUENCY_H
#define LLVM_SUPPORT_BLOCKFREQUENCY_H

#include "llvm/Support/Compiler.h"
#include <cassert>
#include <cstdint>
#include <optional>
Expand All @@ -23,7 +24,7 @@ class raw_ostream;
class BranchProbability;

// This class represents Block Frequency as a 64-bit value.
class BlockFrequency {
class LLVM_ABI BlockFrequency {
uint64_t Frequency;

public:
Expand Down Expand Up @@ -120,7 +121,7 @@ class BlockFrequency {
}
};

void printRelativeBlockFreq(raw_ostream &OS, BlockFrequency EntryFreq,
LLVM_ABI void printRelativeBlockFreq(raw_ostream &OS, BlockFrequency EntryFreq,
BlockFrequency Freq);

} // namespace llvm
Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/Support/BranchProbability.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef LLVM_SUPPORT_BRANCHPROBABILITY_H
#define LLVM_SUPPORT_BRANCHPROBABILITY_H

#include "llvm/Support/Compiler.h"
#include "llvm/Support/DataTypes.h"
#include <algorithm>
#include <cassert>
Expand All @@ -27,7 +28,7 @@ class raw_ostream;
// no greater than 1. It uses a fixed-point-like implementation, in which the
// denominator is always a constant value (here we use 1<<31 for maximum
// precision).
class BranchProbability {
class LLVM_ABI BranchProbability {
// Numerator
uint32_t N;

Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/Support/BuryPointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_SUPPORT_BURYPOINTER_H
#define LLVM_SUPPORT_BURYPOINTER_H

#include "llvm/Support/Compiler.h"
#include <memory>

namespace llvm {
Expand All @@ -19,7 +20,7 @@ namespace llvm {
// the memory is not misdiagnosed as an unintentional leak by leak detection
// tools (this is achieved by preserving pointers to the object in a globally
// visible array).
void BuryPointer(const void *Ptr);
LLVM_ABI void BuryPointer(const void *Ptr);
template <typename T> void BuryPointer(std::unique_ptr<T> Ptr) {
BuryPointer(Ptr.release());
}
Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/Support/COM.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
#ifndef LLVM_SUPPORT_COM_H
#define LLVM_SUPPORT_COM_H

#include "llvm/Support/Compiler.h"
namespace llvm {
namespace sys {

enum class COMThreadingMode { SingleThreaded, MultiThreaded };

class InitializeCOMRAII {
class LLVM_ABI InitializeCOMRAII {
public:
explicit InitializeCOMRAII(COMThreadingMode Threading,
bool SpeedOverMemory = false);
Expand Down
7 changes: 4 additions & 3 deletions llvm/include/llvm/Support/CRC.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@
#ifndef LLVM_SUPPORT_CRC_H
#define LLVM_SUPPORT_CRC_H

#include "llvm/Support/Compiler.h"
#include "llvm/Support/DataTypes.h"

namespace llvm {
template <typename T> class ArrayRef;

// Compute the CRC-32 of Data.
uint32_t crc32(ArrayRef<uint8_t> Data);
LLVM_ABI uint32_t crc32(ArrayRef<uint8_t> Data);

// Compute the running CRC-32 of Data, with CRC being the previous value of the
// checksum.
uint32_t crc32(uint32_t CRC, ArrayRef<uint8_t> Data);
LLVM_ABI uint32_t crc32(uint32_t CRC, ArrayRef<uint8_t> Data);

// Class for computing the JamCRC.
//
Expand All @@ -42,7 +43,7 @@ uint32_t crc32(uint32_t CRC, ArrayRef<uint8_t> Data);
//
// N.B. We permit flexibility of the "Init" value. Some consumers of this need
// it to be zero.
class JamCRC {
class LLVM_ABI JamCRC {
public:
JamCRC(uint32_t Init = 0xFFFFFFFFU) : CRC(Init) {}

Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/Support/CSKYAttributeParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
#define LLVM_SUPPORT_CSKYATTRIBUTEPARSER_H

#include "llvm/Support/CSKYAttributes.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ELFAttributeParser.h"

namespace llvm {
class CSKYAttributeParser : public ELFAttributeParser {
class LLVM_ABI CSKYAttributeParser : public ELFAttributeParser {
struct DisplayHandler {
CSKYAttrs::AttrType attribute;
Error (CSKYAttributeParser::*routine)(unsigned);
Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/Support/CSKYAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
#ifndef LLVM_SUPPORT_CSKYATTRIBUTES_H
#define LLVM_SUPPORT_CSKYATTRIBUTES_H

#include "llvm/Support/Compiler.h"
#include "llvm/Support/ELFAttributes.h"

namespace llvm {
namespace CSKYAttrs {

const TagNameMap &getCSKYAttributeTags();
LLVM_ABI const TagNameMap &getCSKYAttributeTags();

enum AttrType {
CSKY_ARCH_NAME = 4,
Expand Down
Loading
Loading