Skip to content

Commit 45ba0d2

Browse files
Separate out trivial includes and types from llvm.h (#3141)
These can be moved out to places where they're actually used without many/any other changes. This helps compile times and makes this more local. Also move some old Jit/EE enums to the shared file.
1 parent e5a2132 commit 45ba0d2

File tree

9 files changed

+119
-97
lines changed

9 files changed

+119
-97
lines changed

src/coreclr/jit/llvm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#include "jitpch.h"
54
#include "llvm.h"
65

76
// TODO-LLVM-Upstream: figure out how to fix these warnings in LLVM headers.
@@ -12,6 +11,7 @@
1211
#pragma warning (disable : 4267)
1312
#include "llvm/Bitcode/BitcodeWriter.h"
1413
#include "llvm/Support/Signals.h"
14+
#include "llvm/IR/Verifier.h"
1515
#pragma warning(pop)
1616

1717
void* g_callbacks[EEAI_Count];

src/coreclr/jit/llvm.h

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@
2323
#pragma warning(disable : 4459)
2424
#pragma warning(disable : 4702)
2525
#include "llvm/IR/IRBuilder.h"
26-
#include "llvm/IR/DIBuilder.h"
2726
#include "llvm/IR/Function.h"
28-
#include "llvm/IR/Verifier.h"
29-
#include "llvm/IR/IntrinsicsWebAssembly.h"
3027
#pragma warning(pop)
3128

29+
// Forward-declare some LLVM types to avoid including the corresponding header into each compilation unit.
30+
namespace llvm
31+
{
32+
class DIBuilder;
33+
};
34+
3235
using llvm::LLVMContext;
3336
using llvm::Module;
3437
using llvm::Function;
@@ -51,13 +54,6 @@ const int TARGET_POINTER_BITS = TARGET_POINTER_SIZE * BITS_PER_BYTE;
5154

5255
// Part of the Jit/EE interface, must be kept in sync with the managed versions in "CorInfoImpl.Llvm.cs".
5356
//
54-
enum class CorInfoLlvmEHModel
55-
{
56-
Cpp, // Landingpad-based LLVM IR; compatible with Itanium ABI.
57-
Wasm, // WinEH-based LLVM IR; custom WASM EH-based ABI.
58-
Emulated, // Invoke-free LLVM IR; "unwinding" performed via explicit checks and returns
59-
};
60-
6157
struct CORINFO_LLVM_EH_CLAUSE
6258
{
6359
CORINFO_EH_CLAUSE_FLAGS Flags;
@@ -66,11 +62,6 @@ struct CORINFO_LLVM_EH_CLAUSE
6662
unsigned FilterIndex;
6763
};
6864

69-
enum CorInfoLlvmJitTestKind
70-
{
71-
CORINFO_JIT_TEST_LSSA = 1
72-
};
73-
7465
struct CORINFO_LLVM_JIT_TEST_INFO
7566
{
7667
const char* ExpectedLssaAllocation;
@@ -117,7 +108,7 @@ template <typename T>
117108
struct JitStdMallocAllocator
118109
{
119110
MallocAllocator m_alloc;
120-
JitStdMallocAllocator(MallocAllocator alloc) : m_alloc(alloc) { }
111+
JitStdMallocAllocator(MallocAllocator alloc) : m_alloc(alloc) {}
121112

122113
T* allocate(size_t count)
123114
{
@@ -263,35 +254,9 @@ struct PhiPair
263254
llvm::PHINode* LlvmPhiNode;
264255
};
265256

266-
struct LlvmBlockRange
267-
{
268-
llvm::BasicBlock* FirstBlock;
269-
llvm::BasicBlock* LastBlock;
270-
INDEBUG(unsigned Count = 1);
271-
272-
LlvmBlockRange(llvm::BasicBlock* llvmBlock) : FirstBlock(llvmBlock), LastBlock(llvmBlock)
273-
{
274-
}
275-
};
276-
277-
typedef JitHashTable<unsigned, JitSmallPrimitiveKeyFuncs<unsigned>, llvm::AllocaInst*> AllocaMap;
278-
279-
struct FunctionInfo
280-
{
281-
Function* LlvmFunction;
282-
union {
283-
llvm::AllocaInst** Allocas; // Dense "lclNum -> Alloca*" mapping used for the main function.
284-
AllocaMap* AllocaMap; // Sparse "lclNum -> Alloca*" mapping used for funclets.
285-
};
286-
llvm::BasicBlock* ResumeLlvmBlock;
287-
llvm::BasicBlock* ExceptionThrownReturnLlvmBlock;
288-
};
289-
290-
struct EHRegionInfo
291-
{
292-
llvm::BasicBlock* UnwindBlock;
293-
Value* CatchArgValue;
294-
};
257+
struct LlvmBlockRange;
258+
struct EHRegionInfo;
259+
struct FunctionInfo;
295260

296261
class TypeDebugInfoModule;
297262
class SingleThreadedCompilationContext
@@ -684,8 +649,8 @@ class Llvm
684649
CallSiteFacts getCallSiteFactsForHelper(CorInfoHelpFunc helperFunc);
685650
void annotateHelperFunction(CorInfoHelpFunc helperFunc, Function* llvmFunc);
686651
Function* getOrCreateKnownLlvmFunction(StringRef name,
687-
std::function<FunctionType*()> createFunctionType,
688-
std::function<void(Function*)> annotateFunction = [](Function*) { });
652+
std::function<FunctionType* ()> createFunctionType,
653+
std::function<void(Function*)> annotateFunction = [](Function*) {});
689654

690655
EHRegionInfo& getEHRegionInfo(unsigned ehIndex);
691656
llvm::BasicBlock* getUnwindLlvmBlockForCurrentInvoke();
@@ -758,6 +723,8 @@ class Llvm
758723
void declareDebugVariables();
759724
void assignDebugVariable(unsigned lclNum, Value* value);
760725

726+
void finalizeDebugInfo();
727+
761728
unsigned getLineNumberForILOffset(unsigned ilOffset);
762729
llvm::DILocation* getDebugLocation(unsigned lineNo);
763730
llvm::DILocation* getArtificialDebugLocation();

src/coreclr/jit/llvmcodegen.cpp

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,46 @@
77

88
#include "llvm.h"
99

10+
// TODO-LLVM-Upstream: figure out how to fix these warnings in LLVM headers.
11+
#pragma warning(push)
12+
#pragma warning(disable : 4242)
13+
#pragma warning(disable : 4244)
14+
#include "llvm/IR/Verifier.h"
15+
#include "llvm/IR/IntrinsicsWebAssembly.h"
16+
#pragma warning(pop)
17+
1018
#define BBNAME(prefix, index) Twine(prefix) + ((index < 10) ? "0" : "") + Twine(index)
1119

20+
using AllocaMap = JitHashTable<unsigned, JitSmallPrimitiveKeyFuncs<unsigned>, llvm::AllocaInst*>;
21+
22+
struct LlvmBlockRange
23+
{
24+
llvm::BasicBlock* FirstBlock;
25+
llvm::BasicBlock* LastBlock;
26+
INDEBUG(unsigned Count = 1);
27+
28+
LlvmBlockRange(llvm::BasicBlock* llvmBlock) : FirstBlock(llvmBlock), LastBlock(llvmBlock)
29+
{
30+
}
31+
};
32+
33+
struct EHRegionInfo
34+
{
35+
llvm::BasicBlock* UnwindBlock;
36+
Value* CatchArgValue;
37+
};
38+
39+
struct FunctionInfo
40+
{
41+
Function* LlvmFunction;
42+
union {
43+
llvm::AllocaInst** Allocas; // Dense "lclNum -> Alloca*" mapping used for the main function.
44+
AllocaMap* AllocaMap; // Sparse "lclNum -> Alloca*" mapping used for funclets.
45+
};
46+
llvm::BasicBlock* ResumeLlvmBlock;
47+
llvm::BasicBlock* ExceptionThrownReturnLlvmBlock;
48+
};
49+
1250
//------------------------------------------------------------------------
1351
// Compile: Compile IR to LLVM, adding to the LLVM Module
1452
//
@@ -25,12 +63,7 @@ void Llvm::Compile()
2563
generateUnwindBlocks();
2664
generateBlocks();
2765
fillPhis();
28-
29-
if (m_diFunction != nullptr)
30-
{
31-
m_diBuilder->finalizeSubprogram(m_diFunction);
32-
}
33-
66+
finalizeDebugInfo();
3467
generateAuxiliaryArtifacts();
3568

3669
displayGeneratedCode();
@@ -398,7 +431,7 @@ void Llvm::generateUnwindBlocks()
398431
llvm::Constant* nullValue = llvm::Constant::getNullValue(ptrLlvmType);
399432
Function* personalityLlvmFunc = getOrCreatePersonalityLlvmFunction(m_ehModel);
400433
Function* cppBeginCatchFunc = nullptr;
401-
if (model == CorInfoLlvmEHModel::Cpp)
434+
if (model == CORINFO_LLVM_EH_CPP)
402435
{
403436
cppBeginCatchFunc = getOrCreateKnownLlvmFunction("__cxa_begin_catch", [ptrLlvmType]() {
404437
return FunctionType::get(ptrLlvmType, {ptrLlvmType}, /* isVarArg */ false);
@@ -465,7 +498,7 @@ void Llvm::generateUnwindBlocks()
465498

466499
// The code we will generate uses native unwinding to call second-pass handlers.
467500
//
468-
// For CorInfoLlvmEHModel::Cpp:
501+
// For CORINFO_LLVM_EH_CPP:
469502
//
470503
// UNWIND_INNER:
471504
// __cxa_begin_catch(landingPadInst.ExceptionData);
@@ -482,7 +515,7 @@ void Llvm::generateUnwindBlocks()
482515
// RESUME:
483516
// resume(cppExcTuple); // Rethrow the exception and unwind to caller.
484517
//
485-
// CorInfoLlvmEHModel::Wasm has the same structure but uses Windows EH instructions and rethrows:
518+
// CORINFO_LLVM_EH_WASM has the same structure but uses Windows EH instructions and rethrows:
486519
//
487520
// UNWIND_INNER:
488521
// catchswitch unwind to UNWIND_OUTER
@@ -494,7 +527,7 @@ void Llvm::generateUnwindBlocks()
494527
// }
495528
// <Catch handler code...>
496529
//
497-
// CorInfoLlvmEHModel::Emulated uses fully "manual" unwinding based on early returns:
530+
// CORINFO_LLVM_EH_EMULATED uses fully "manual" unwinding based on early returns:
498531
//
499532
// UNWIND_INNER:
500533
// RhpExceptionThrown = 0; // Stop the unwinding before calling managed code.
@@ -517,7 +550,7 @@ void Llvm::generateUnwindBlocks()
517550
// Create the C++ exception data alloca, to store the active landing pad value.
518551
FunctionInfo& funcInfo = getLlvmFunctionInfoForIndex(funcIdx);
519552
llvm::AllocaInst* cppExcTupleAlloca = funcData.CppExcTupleAlloca;
520-
if ((model == CorInfoLlvmEHModel::Cpp) && (cppExcTupleAlloca == nullptr))
553+
if ((model == CORINFO_LLVM_EH_CPP) && (cppExcTupleAlloca == nullptr))
521554
{
522555
llvm::BasicBlock* prologLlvmBlock = getOrCreatePrologLlvmBlockForFunction(funcIdx);
523556

@@ -529,15 +562,15 @@ void Llvm::generateUnwindBlocks()
529562

530563
// Generate the resume block needed in the C++ and emulated models.
531564
if ((funcInfo.ResumeLlvmBlock == nullptr) &&
532-
((model == CorInfoLlvmEHModel::Cpp) || (model == CorInfoLlvmEHModel::Emulated)))
565+
((model == CORINFO_LLVM_EH_CPP) || (model == CORINFO_LLVM_EH_EMULATED)))
533566
{
534567
llvm::BasicBlock* resumeLlvmBlock =
535568
llvm::BasicBlock::Create(m_context->Context, "BBRE", llvmFunc, funcData.InsertBeforeLlvmBlock);
536569
LlvmBlockRange resumeLlvmBlocks(resumeLlvmBlock);
537570
setCurrentEmitContext(
538571
funcIdx, EHblkDsc::NO_ENCLOSING_INDEX, EHblkDsc::NO_ENCLOSING_INDEX, &resumeLlvmBlocks);
539572

540-
if (model == CorInfoLlvmEHModel::Cpp)
573+
if (model == CORINFO_LLVM_EH_CPP)
541574
{
542575
Value* resumeOperandValue = _builder.CreateLoad(cppExcTupleLlvmType, cppExcTupleAlloca);
543576
_builder.CreateResume(resumeOperandValue);
@@ -561,9 +594,9 @@ void Llvm::generateUnwindBlocks()
561594
_builder.SetCurrentDebugLocation(unwindBlocksDebugLoc);
562595

563596
// Set up entry to the native "catch".
564-
if ((model == CorInfoLlvmEHModel::Cpp) || (model == CorInfoLlvmEHModel::Emulated))
597+
if ((model == CORINFO_LLVM_EH_CPP) || (model == CORINFO_LLVM_EH_EMULATED))
565598
{
566-
if (model == CorInfoLlvmEHModel::Cpp)
599+
if (model == CORINFO_LLVM_EH_CPP)
567600
{
568601
llvm::LandingPadInst* landingPadInst = _builder.CreateLandingPad(cppExcTupleLlvmType, 1);
569602
landingPadInst->addClause(nullValue); // Catch all C++ exceptions.
@@ -2275,11 +2308,11 @@ void Llvm::buildCatchRet(BasicBlock* block)
22752308
llvm::BasicBlock* destLlvmBlock = getFirstLlvmBlockForBlock(block->GetTarget());
22762309
switch (m_ehModel)
22772310
{
2278-
case CorInfoLlvmEHModel::Cpp:
2279-
case CorInfoLlvmEHModel::Emulated:
2311+
case CORINFO_LLVM_EH_CPP:
2312+
case CORINFO_LLVM_EH_EMULATED:
22802313
_builder.CreateBr(destLlvmBlock);
22812314
break;
2282-
case CorInfoLlvmEHModel::Wasm:
2315+
case CORINFO_LLVM_EH_WASM:
22832316
_builder.CreateCatchRet(getCatchPadForHandler(block->getHndIndex()), destLlvmBlock);
22842317
break;
22852318
default:
@@ -2688,7 +2721,7 @@ llvm::CallBase* Llvm::emitCallOrInvoke(llvm::FunctionCallee callee, ArrayRef<Val
26882721
llvm::BasicBlock* catchLlvmBlock = getUnwindLlvmBlockForCurrentInvoke();
26892722

26902723
llvm::SmallVector<llvm::OperandBundleDef, 1> bundles{};
2691-
if (m_ehModel == CorInfoLlvmEHModel::Wasm)
2724+
if (m_ehModel == CORINFO_LLVM_EH_WASM)
26922725
{
26932726
llvm::CatchPadInst* catchPadInst = getCatchPadForHandler(getCurrentHandlerIndex());
26942727
if ((catchPadInst != nullptr) && (catchPadInst->getFunction() == getCurrentLlvmFunction()))
@@ -2699,7 +2732,7 @@ llvm::CallBase* Llvm::emitCallOrInvoke(llvm::FunctionCallee callee, ArrayRef<Val
26992732

27002733
llvm::CallBase* callInst;
27012734
bool isThrowingCall = (callSiteFacts & CSF_NO_THROW) == 0;
2702-
if (isThrowingCall && (catchLlvmBlock != nullptr) && (m_ehModel != CorInfoLlvmEHModel::Emulated))
2735+
if (isThrowingCall && (catchLlvmBlock != nullptr) && (m_ehModel != CORINFO_LLVM_EH_EMULATED))
27032736
{
27042737
llvm::BasicBlock* nextLlvmBlock = createInlineLlvmBlock();
27052738
callInst = _builder.CreateInvoke(callee, nextLlvmBlock, catchLlvmBlock, args, bundles);
@@ -2715,7 +2748,7 @@ llvm::CallBase* Llvm::emitCallOrInvoke(llvm::FunctionCallee callee, ArrayRef<Val
27152748
}
27162749
}
27172750

2718-
if (isThrowingCall && (m_ehModel == CorInfoLlvmEHModel::Emulated))
2751+
if (isThrowingCall && (m_ehModel == CORINFO_LLVM_EH_EMULATED))
27192752
{
27202753
// In the emulated EH model, top-level calls also need to return early if they throw.
27212754
if (catchLlvmBlock == nullptr)
@@ -2932,7 +2965,7 @@ void Llvm::annotateHelperFunction(CorInfoHelpFunc helperFunc, Function* llvmFunc
29322965
}
29332966

29342967
HelperCallProperties& properties = Compiler::s_helperCallProperties;
2935-
const bool isEmulatedEH = m_ehModel == CorInfoLlvmEHModel::Emulated;
2968+
const bool isEmulatedEH = m_ehModel == CORINFO_LLVM_EH_EMULATED;
29362969
const bool mayThrow = helperCallMayPhysicallyThrow(helperFunc);
29372970

29382971
if (!mayThrow)
@@ -3026,7 +3059,7 @@ llvm::BasicBlock* Llvm::getUnwindLlvmBlockForCurrentInvoke()
30263059

30273060
void Llvm::emitUnwindToOuterHandler()
30283061
{
3029-
if (m_ehModel == CorInfoLlvmEHModel::Wasm)
3062+
if (m_ehModel == CORINFO_LLVM_EH_WASM)
30303063
{
30313064
Function* wasmRethrowLlvmFunc =
30323065
llvm::Intrinsic::getDeclaration(&m_context->Module, llvm::Intrinsic::wasm_rethrow);
@@ -3078,19 +3111,19 @@ Function* Llvm::getOrCreatePersonalityLlvmFunction(CorInfoLlvmEHModel ehModel)
30783111
{
30793112
switch (ehModel)
30803113
{
3081-
case CorInfoLlvmEHModel::Cpp:
3114+
case CORINFO_LLVM_EH_CPP:
30823115
return getOrCreateKnownLlvmFunction("__gxx_personality_v0", [this]() {
30833116
Type* ptrLlvmType = getPtrLlvmType();
30843117
Type* int32LlvmType = Type::getInt32Ty(m_context->Context);
30853118
Type* cppExcTupleLlvmType = llvm::StructType::get(ptrLlvmType, int32LlvmType);
30863119
return FunctionType::get(cppExcTupleLlvmType, {int32LlvmType, ptrLlvmType, ptrLlvmType}, /* isVarArg */ true);
30873120
});
30883121
break;
3089-
case CorInfoLlvmEHModel::Wasm:
3122+
case CORINFO_LLVM_EH_WASM:
30903123
return getOrCreateKnownLlvmFunction("__gxx_wasm_personality_v0", [this]() {
30913124
return FunctionType::get(Type::getInt32Ty(m_context->Context), /* isVarArg */ true);
30923125
});
3093-
case CorInfoLlvmEHModel::Emulated:
3126+
case CORINFO_LLVM_EH_EMULATED:
30943127
return nullptr;
30953128
default:
30963129
unreached();
@@ -3099,7 +3132,7 @@ Function* Llvm::getOrCreatePersonalityLlvmFunction(CorInfoLlvmEHModel ehModel)
30993132

31003133
llvm::CatchPadInst* Llvm::getCatchPadForHandler(unsigned hndIndex)
31013134
{
3102-
assert(m_ehModel == CorInfoLlvmEHModel::Wasm);
3135+
assert(m_ehModel == CORINFO_LLVM_EH_WASM);
31033136
if (hndIndex == EHblkDsc::NO_ENCLOSING_INDEX)
31043137
{
31053138
return nullptr;
@@ -3120,7 +3153,7 @@ llvm::CatchPadInst* Llvm::getCatchPadForHandler(unsigned hndIndex)
31203153

31213154
llvm::BasicBlock* Llvm::getOrCreateExceptionThrownReturnBlock()
31223155
{
3123-
assert(m_ehModel == CorInfoLlvmEHModel::Emulated);
3156+
assert(m_ehModel == CORINFO_LLVM_EH_EMULATED);
31243157

31253158
FunctionInfo& funcInfo = getLlvmFunctionInfoForIndex(getCurrentLlvmFunctionIndex());
31263159
if (funcInfo.ExceptionThrownReturnLlvmBlock == nullptr)
@@ -3144,7 +3177,7 @@ llvm::BasicBlock* Llvm::getOrCreateExceptionThrownReturnBlock()
31443177

31453178
Value* Llvm::getOrCreateExceptionThrownAddressValue()
31463179
{
3147-
assert(m_ehModel == CorInfoLlvmEHModel::Emulated);
3180+
assert(m_ehModel == CORINFO_LLVM_EH_EMULATED);
31483181
if (m_exceptionThrownAddressValue == nullptr)
31493182
{
31503183
m_exceptionThrownAddressValue = getOrCreateSymbol(GetExceptionThrownVariable(), /* isThreadLocal */ true);

0 commit comments

Comments
 (0)