Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4b198d6

Browse files
committedNov 9, 2024
Auto merge of #132584 - Zalathar:includes, r=cuviper
Trim and tidy includes in `rustc_llvm` These includes tend to accumulate over time, and are usually only removed when something breaks in a new LLVM version, so it's nice to clean them up manually once in a while. General strategy used for this PR: - Remove all includes from `LLVMWrapper.h` that aren't needed by the header itself, transplanting them to individual source files as necessary. - For each source file, temporarily remove each include if doing so doesn't cause a compile error. - If a “required” include looks like it shouldn't be needed, try replacing it with its sub-includes, then trim that list. - After doing all of the above, go back and re-add any removed include if the file does actually use things defined in that header, even if the header happens to also be included by something else.
2 parents 62bb2ac + 920d277 commit 4b198d6

File tree

7 files changed

+145
-164
lines changed

7 files changed

+145
-164
lines changed
 

‎compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ pub enum DLLStorageClass {
204204
DllExport = 2, // Function to be accessible from DLL.
205205
}
206206

207-
/// Matches LLVMRustAttribute in LLVMWrapper.h
207+
/// Must match the layout of `LLVMRustAttributeKind`.
208208
/// Semantically a subset of the C++ enum llvm::Attribute::AttrKind,
209209
/// though it is not ABI compatible (since it's a C++ enum)
210210
#[repr(C)]

‎compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include "LLVMWrapper.h"
2+
23
#include "llvm/ADT/ArrayRef.h"
4+
#include "llvm/ADT/SmallVector.h"
5+
#include "llvm/ADT/StringRef.h"
6+
#include "llvm/IR/Module.h"
37
#include "llvm/ProfileData/Coverage/CoverageMapping.h"
48
#include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
59
#include "llvm/ProfileData/InstrProf.h"
Lines changed: 9 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,23 @@
1+
#ifndef INCLUDED_RUSTC_LLVM_LLVMWRAPPER_H
2+
#define INCLUDED_RUSTC_LLVM_LLVMWRAPPER_H
3+
14
#include "SuppressLLVMWarnings.h"
25

3-
#include "llvm-c/BitReader.h"
4-
#include "llvm-c/Core.h"
5-
#include "llvm-c/Object.h"
6-
#include "llvm/ADT/ArrayRef.h"
7-
#include "llvm/ADT/DenseSet.h"
8-
#include "llvm/ADT/SmallVector.h"
9-
#include "llvm/Analysis/Lint.h"
10-
#include "llvm/Analysis/Passes.h"
11-
#include "llvm/IR/IRBuilder.h"
12-
#include "llvm/IR/InlineAsm.h"
13-
#include "llvm/IR/LLVMContext.h"
14-
#include "llvm/IR/Module.h"
15-
#include "llvm/Support/CommandLine.h"
16-
#include "llvm/Support/Debug.h"
17-
#include "llvm/Support/DynamicLibrary.h"
18-
#include "llvm/Support/FormattedStream.h"
19-
#include "llvm/Support/JSON.h"
20-
#include "llvm/Support/Memory.h"
21-
#include "llvm/Support/SourceMgr.h"
22-
#include "llvm/Support/TargetSelect.h"
23-
#include "llvm/Support/Timer.h"
24-
#include "llvm/Support/raw_ostream.h"
25-
#include "llvm/Target/TargetMachine.h"
26-
#include "llvm/Target/TargetOptions.h"
27-
#include "llvm/Transforms/IPO.h"
28-
#include "llvm/Transforms/Scalar.h"
6+
#include "llvm/Config/llvm-config.h" // LLVM_VERSION_MAJOR, LLVM_VERSION_MINOR
7+
#include "llvm/Support/raw_ostream.h" // llvm::raw_ostream
8+
#include <cstddef> // size_t etc
9+
#include <cstdint> // uint64_t etc
2910

3011
#define LLVM_VERSION_GE(major, minor) \
3112
(LLVM_VERSION_MAJOR > (major) || \
3213
LLVM_VERSION_MAJOR == (major) && LLVM_VERSION_MINOR >= (minor))
3314

3415
#define LLVM_VERSION_LT(major, minor) (!LLVM_VERSION_GE((major), (minor)))
3516

36-
#if LLVM_VERSION_GE(20, 0)
37-
#include "llvm/Transforms/Utils/Instrumentation.h"
38-
#else
39-
#include "llvm/Transforms/Instrumentation.h"
40-
#endif
41-
42-
#include "llvm/IR/LegacyPassManager.h"
43-
44-
#include "llvm/Bitcode/BitcodeReader.h"
45-
#include "llvm/Bitcode/BitcodeWriter.h"
46-
47-
#include "llvm/IR/DIBuilder.h"
48-
#include "llvm/IR/DebugInfo.h"
49-
#include "llvm/IR/IRPrintingPasses.h"
50-
#include "llvm/Linker/Linker.h"
51-
52-
#include "llvm/TargetParser/Triple.h"
53-
5417
extern "C" void LLVMRustSetLastError(const char *);
5518

5619
enum class LLVMRustResult { Success, Failure };
5720

58-
enum LLVMRustAttribute {
59-
AlwaysInline = 0,
60-
ByVal = 1,
61-
Cold = 2,
62-
InlineHint = 3,
63-
MinSize = 4,
64-
Naked = 5,
65-
NoAlias = 6,
66-
NoCapture = 7,
67-
NoInline = 8,
68-
NonNull = 9,
69-
NoRedZone = 10,
70-
NoReturn = 11,
71-
NoUnwind = 12,
72-
OptimizeForSize = 13,
73-
ReadOnly = 14,
74-
SExt = 15,
75-
StructRet = 16,
76-
UWTable = 17,
77-
ZExt = 18,
78-
InReg = 19,
79-
SanitizeThread = 20,
80-
SanitizeAddress = 21,
81-
SanitizeMemory = 22,
82-
NonLazyBind = 23,
83-
OptimizeNone = 24,
84-
ReadNone = 26,
85-
SanitizeHWAddress = 28,
86-
WillReturn = 29,
87-
StackProtectReq = 30,
88-
StackProtectStrong = 31,
89-
StackProtect = 32,
90-
NoUndef = 33,
91-
SanitizeMemTag = 34,
92-
NoCfCheck = 35,
93-
ShadowCallStack = 36,
94-
AllocSize = 37,
95-
AllocatedPointer = 38,
96-
AllocAlign = 39,
97-
SanitizeSafeStack = 40,
98-
FnRetThunkExtern = 41,
99-
Writable = 42,
100-
DeadOnUnwind = 43,
101-
};
102-
10321
typedef struct OpaqueRustString *RustStringRef;
10422
typedef struct LLVMOpaqueTwine *LLVMTwineRef;
10523
typedef struct LLVMOpaqueSMDiagnostic *LLVMSMDiagnosticRef;
@@ -127,3 +45,5 @@ class RawRustStringOstream : public llvm::raw_ostream {
12745
flush();
12846
}
12947
};
48+
49+
#endif // INCLUDED_RUSTC_LLVM_LLVMWRAPPER_H

‎compiler/rustc_llvm/llvm-wrapper/Linker.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
#include "llvm/Linker/Linker.h"
2-
#include "SuppressLLVMWarnings.h"
3-
41
#include "LLVMWrapper.h"
52

3+
#include "llvm/Bitcode/BitcodeReader.h"
4+
#include "llvm/IR/Module.h"
5+
#include "llvm/Linker/Linker.h"
6+
#include "llvm/Support/MemoryBuffer.h"
7+
68
using namespace llvm;
79

810
struct RustLinker {

‎compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
#include <stdio.h>
2-
3-
#include <cstddef>
4-
#include <iomanip>
5-
#include <set>
6-
#include <vector>
7-
81
#include "LLVMWrapper.h"
92

10-
#include "llvm/Analysis/AliasAnalysis.h"
3+
#include "llvm-c/Core.h"
4+
#include "llvm/ADT/ArrayRef.h"
5+
#include "llvm/ADT/DenseSet.h"
6+
#include "llvm/ADT/SmallVector.h"
7+
#include "llvm/Analysis/Lint.h"
118
#include "llvm/Analysis/TargetLibraryInfo.h"
12-
#include "llvm/Analysis/TargetTransformInfo.h"
139
#include "llvm/Bitcode/BitcodeWriter.h"
1410
#include "llvm/CodeGen/CommandFlags.h"
15-
#include "llvm/CodeGen/TargetSubtargetInfo.h"
1611
#include "llvm/IR/AssemblyAnnotationWriter.h"
1712
#include "llvm/IR/AutoUpgrade.h"
18-
#include "llvm/IR/IntrinsicInst.h"
13+
#include "llvm/IR/LegacyPassManager.h"
14+
#include "llvm/IR/PassManager.h"
1915
#include "llvm/IR/Verifier.h"
2016
#include "llvm/LTO/LTO.h"
17+
#include "llvm/MC/MCSubtargetInfo.h"
2118
#include "llvm/MC/TargetRegistry.h"
22-
#include "llvm/Object/IRObjectFile.h"
2319
#include "llvm/Object/ObjectFile.h"
2420
#include "llvm/Passes/PassBuilder.h"
2521
#include "llvm/Passes/PassPlugin.h"
@@ -30,25 +26,28 @@
3026
#include "llvm/Support/VirtualFileSystem.h"
3127
#include "llvm/Target/TargetMachine.h"
3228
#include "llvm/TargetParser/Host.h"
33-
#include "llvm/Transforms/IPO/AlwaysInliner.h"
3429
#include "llvm/Transforms/IPO/FunctionImport.h"
3530
#include "llvm/Transforms/IPO/Internalize.h"
3631
#include "llvm/Transforms/IPO/LowerTypeTests.h"
3732
#include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
3833
#include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
3934
#include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h"
40-
#include "llvm/Transforms/Utils/AddDiscriminators.h"
41-
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
42-
#if LLVM_VERSION_GE(19, 0)
43-
#include "llvm/Support/PGOOptions.h"
44-
#endif
4535
#include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
4636
#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
4737
#include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
4838
#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
49-
#include "llvm/Transforms/Utils.h"
5039
#include "llvm/Transforms/Utils/CanonicalizeAliases.h"
40+
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
5141
#include "llvm/Transforms/Utils/NameAnonGlobals.h"
42+
#include <set>
43+
#include <string>
44+
#include <vector>
45+
46+
// Conditional includes prevent clang-format from fully sorting the list,
47+
// so keep them separate.
48+
#if LLVM_VERSION_GE(19, 0)
49+
#include "llvm/Support/PGOOptions.h"
50+
#endif
5251

5352
using namespace llvm;
5453

‎compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 106 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
#include "LLVMWrapper.h"
2+
3+
#include "llvm-c/Core.h"
4+
#include "llvm/ADT/ArrayRef.h"
5+
#include "llvm/ADT/SmallVector.h"
26
#include "llvm/ADT/Statistic.h"
7+
#include "llvm/ADT/StringRef.h"
8+
#include "llvm/BinaryFormat/Magic.h"
39
#include "llvm/Bitcode/BitcodeWriter.h"
10+
#include "llvm/IR/DIBuilder.h"
411
#include "llvm/IR/DebugInfoMetadata.h"
512
#include "llvm/IR/DiagnosticHandler.h"
613
#include "llvm/IR/DiagnosticInfo.h"
714
#include "llvm/IR/DiagnosticPrinter.h"
815
#include "llvm/IR/GlobalVariable.h"
16+
#include "llvm/IR/IRBuilder.h"
17+
#include "llvm/IR/InlineAsm.h"
918
#include "llvm/IR/Instructions.h"
10-
#include "llvm/IR/Intrinsics.h"
1119
#include "llvm/IR/IntrinsicsARM.h"
20+
#include "llvm/IR/LLVMContext.h"
1221
#include "llvm/IR/LLVMRemarkStreamer.h"
1322
#include "llvm/IR/Mangler.h"
23+
#include "llvm/IR/Module.h"
1424
#include "llvm/IR/Value.h"
15-
#include "llvm/Object/Archive.h"
1625
#include "llvm/Object/COFFImportFile.h"
17-
#include "llvm/Object/ObjectFile.h"
18-
#include "llvm/Pass.h"
1926
#include "llvm/Remarks/RemarkFormat.h"
2027
#include "llvm/Remarks/RemarkSerializer.h"
2128
#include "llvm/Remarks/RemarkStreamer.h"
29+
#include "llvm/Support/Compression.h"
30+
#include "llvm/Support/FileSystem.h"
31+
#include "llvm/Support/JSON.h"
2232
#include "llvm/Support/ModRef.h"
2333
#include "llvm/Support/Signals.h"
34+
#include "llvm/Support/Timer.h"
2435
#include "llvm/Support/ToolOutputFile.h"
25-
2636
#include <iostream>
2737

2838
// for raw `write` in the bad-alloc handler
@@ -216,94 +226,140 @@ extern "C" LLVMValueRef LLVMRustInsertPrivateGlobal(LLVMModuleRef M,
216226
GlobalValue::PrivateLinkage, nullptr));
217227
}
218228

219-
static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) {
229+
// Must match the layout of `rustc_codegen_llvm::llvm::ffi::AttributeKind`.
230+
enum class LLVMRustAttributeKind {
231+
AlwaysInline = 0,
232+
ByVal = 1,
233+
Cold = 2,
234+
InlineHint = 3,
235+
MinSize = 4,
236+
Naked = 5,
237+
NoAlias = 6,
238+
NoCapture = 7,
239+
NoInline = 8,
240+
NonNull = 9,
241+
NoRedZone = 10,
242+
NoReturn = 11,
243+
NoUnwind = 12,
244+
OptimizeForSize = 13,
245+
ReadOnly = 14,
246+
SExt = 15,
247+
StructRet = 16,
248+
UWTable = 17,
249+
ZExt = 18,
250+
InReg = 19,
251+
SanitizeThread = 20,
252+
SanitizeAddress = 21,
253+
SanitizeMemory = 22,
254+
NonLazyBind = 23,
255+
OptimizeNone = 24,
256+
ReadNone = 26,
257+
SanitizeHWAddress = 28,
258+
WillReturn = 29,
259+
StackProtectReq = 30,
260+
StackProtectStrong = 31,
261+
StackProtect = 32,
262+
NoUndef = 33,
263+
SanitizeMemTag = 34,
264+
NoCfCheck = 35,
265+
ShadowCallStack = 36,
266+
AllocSize = 37,
267+
AllocatedPointer = 38,
268+
AllocAlign = 39,
269+
SanitizeSafeStack = 40,
270+
FnRetThunkExtern = 41,
271+
Writable = 42,
272+
DeadOnUnwind = 43,
273+
};
274+
275+
static Attribute::AttrKind fromRust(LLVMRustAttributeKind Kind) {
220276
switch (Kind) {
221-
case AlwaysInline:
277+
case LLVMRustAttributeKind::AlwaysInline:
222278
return Attribute::AlwaysInline;
223-
case ByVal:
279+
case LLVMRustAttributeKind::ByVal:
224280
return Attribute::ByVal;
225-
case Cold:
281+
case LLVMRustAttributeKind::Cold:
226282
return Attribute::Cold;
227-
case InlineHint:
283+
case LLVMRustAttributeKind::InlineHint:
228284
return Attribute::InlineHint;
229-
case MinSize:
285+
case LLVMRustAttributeKind::MinSize:
230286
return Attribute::MinSize;
231-
case Naked:
287+
case LLVMRustAttributeKind::Naked:
232288
return Attribute::Naked;
233-
case NoAlias:
289+
case LLVMRustAttributeKind::NoAlias:
234290
return Attribute::NoAlias;
235-
case NoCapture:
291+
case LLVMRustAttributeKind::NoCapture:
236292
return Attribute::NoCapture;
237-
case NoCfCheck:
293+
case LLVMRustAttributeKind::NoCfCheck:
238294
return Attribute::NoCfCheck;
239-
case NoInline:
295+
case LLVMRustAttributeKind::NoInline:
240296
return Attribute::NoInline;
241-
case NonNull:
297+
case LLVMRustAttributeKind::NonNull:
242298
return Attribute::NonNull;
243-
case NoRedZone:
299+
case LLVMRustAttributeKind::NoRedZone:
244300
return Attribute::NoRedZone;
245-
case NoReturn:
301+
case LLVMRustAttributeKind::NoReturn:
246302
return Attribute::NoReturn;
247-
case NoUnwind:
303+
case LLVMRustAttributeKind::NoUnwind:
248304
return Attribute::NoUnwind;
249-
case OptimizeForSize:
305+
case LLVMRustAttributeKind::OptimizeForSize:
250306
return Attribute::OptimizeForSize;
251-
case ReadOnly:
307+
case LLVMRustAttributeKind::ReadOnly:
252308
return Attribute::ReadOnly;
253-
case SExt:
309+
case LLVMRustAttributeKind::SExt:
254310
return Attribute::SExt;
255-
case StructRet:
311+
case LLVMRustAttributeKind::StructRet:
256312
return Attribute::StructRet;
257-
case UWTable:
313+
case LLVMRustAttributeKind::UWTable:
258314
return Attribute::UWTable;
259-
case ZExt:
315+
case LLVMRustAttributeKind::ZExt:
260316
return Attribute::ZExt;
261-
case InReg:
317+
case LLVMRustAttributeKind::InReg:
262318
return Attribute::InReg;
263-
case SanitizeThread:
319+
case LLVMRustAttributeKind::SanitizeThread:
264320
return Attribute::SanitizeThread;
265-
case SanitizeAddress:
321+
case LLVMRustAttributeKind::SanitizeAddress:
266322
return Attribute::SanitizeAddress;
267-
case SanitizeMemory:
323+
case LLVMRustAttributeKind::SanitizeMemory:
268324
return Attribute::SanitizeMemory;
269-
case NonLazyBind:
325+
case LLVMRustAttributeKind::NonLazyBind:
270326
return Attribute::NonLazyBind;
271-
case OptimizeNone:
327+
case LLVMRustAttributeKind::OptimizeNone:
272328
return Attribute::OptimizeNone;
273-
case ReadNone:
329+
case LLVMRustAttributeKind::ReadNone:
274330
return Attribute::ReadNone;
275-
case SanitizeHWAddress:
331+
case LLVMRustAttributeKind::SanitizeHWAddress:
276332
return Attribute::SanitizeHWAddress;
277-
case WillReturn:
333+
case LLVMRustAttributeKind::WillReturn:
278334
return Attribute::WillReturn;
279-
case StackProtectReq:
335+
case LLVMRustAttributeKind::StackProtectReq:
280336
return Attribute::StackProtectReq;
281-
case StackProtectStrong:
337+
case LLVMRustAttributeKind::StackProtectStrong:
282338
return Attribute::StackProtectStrong;
283-
case StackProtect:
339+
case LLVMRustAttributeKind::StackProtect:
284340
return Attribute::StackProtect;
285-
case NoUndef:
341+
case LLVMRustAttributeKind::NoUndef:
286342
return Attribute::NoUndef;
287-
case SanitizeMemTag:
343+
case LLVMRustAttributeKind::SanitizeMemTag:
288344
return Attribute::SanitizeMemTag;
289-
case ShadowCallStack:
345+
case LLVMRustAttributeKind::ShadowCallStack:
290346
return Attribute::ShadowCallStack;
291-
case AllocSize:
347+
case LLVMRustAttributeKind::AllocSize:
292348
return Attribute::AllocSize;
293-
case AllocatedPointer:
349+
case LLVMRustAttributeKind::AllocatedPointer:
294350
return Attribute::AllocatedPointer;
295-
case AllocAlign:
351+
case LLVMRustAttributeKind::AllocAlign:
296352
return Attribute::AllocAlign;
297-
case SanitizeSafeStack:
353+
case LLVMRustAttributeKind::SanitizeSafeStack:
298354
return Attribute::SafeStack;
299-
case FnRetThunkExtern:
355+
case LLVMRustAttributeKind::FnRetThunkExtern:
300356
return Attribute::FnRetThunkExtern;
301-
case Writable:
357+
case LLVMRustAttributeKind::Writable:
302358
return Attribute::Writable;
303-
case DeadOnUnwind:
359+
case LLVMRustAttributeKind::DeadOnUnwind:
304360
return Attribute::DeadOnUnwind;
305361
}
306-
report_fatal_error("bad AttributeKind");
362+
report_fatal_error("bad LLVMRustAttributeKind");
307363
}
308364

309365
template <typename T>
@@ -333,7 +389,7 @@ extern "C" void LLVMRustAddCallSiteAttributes(LLVMValueRef Instr,
333389
}
334390

335391
extern "C" LLVMAttributeRef
336-
LLVMRustCreateAttrNoValue(LLVMContextRef C, LLVMRustAttribute RustAttr) {
392+
LLVMRustCreateAttrNoValue(LLVMContextRef C, LLVMRustAttributeKind RustAttr) {
337393
return wrap(Attribute::get(*unwrap(C), fromRust(RustAttr)));
338394
}
339395

‎compiler/rustc_llvm/llvm-wrapper/SymbolWrapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
// * https://github.com/llvm/llvm-project/blob/ef6d1ec07c693352c4a60dd58db08d2d8558f6ea/llvm/lib/Object/ArchiveWriter.cpp
99

1010
#include "LLVMWrapper.h"
11-
#include "SuppressLLVMWarnings.h"
11+
1212
#include "llvm/ADT/SmallString.h"
1313
#include "llvm/IR/LLVMContext.h"
1414
#include "llvm/Object/COFF.h"
1515
#include "llvm/Object/COFFImportFile.h"
1616
#include "llvm/Object/IRObjectFile.h"
1717
#include "llvm/Object/ObjectFile.h"
18-
#include <llvm/Support/raw_ostream.h>
18+
#include "llvm/Support/raw_ostream.h"
1919

2020
using namespace llvm;
2121
using namespace llvm::sys;

0 commit comments

Comments
 (0)
Please sign in to comment.