summaryrefslogtreecommitdiff
path: root/source/slang/slang-lower-to-ir.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2020-05-28 14:01:51 -0400
committerGitHub <noreply@github.com>2020-05-28 14:01:51 -0400
commitc2d31347ea06c768045e7c503ef0188e0e5356de (patch)
tree1a4ee67aafca0a709ae691104023431bb6829825 /source/slang/slang-lower-to-ir.cpp
parente5d0f3360f44a4cdd2390e7817db17bb3cc0dd04 (diff)
WIP: ASTBuilder (#1358)
* Compiles. * Small tidy up around session/ASTBuilder. * Tests are now passing. * Fix Visual Studio project. * Fix using new X to use builder when protectedness of Ctor is not enough. Substitute->substitute * Add some missing ast nodes created outside of ASTBuilder. * Compile time check that ASTBuilder is making an AST type. * Moced findClasInfo and findSyntaxClass (essentially the same thing) to SharedASTBuilder from Session.
Diffstat (limited to 'source/slang/slang-lower-to-ir.cpp')
-rw-r--r--source/slang/slang-lower-to-ir.cpp61
1 files changed, 32 insertions, 29 deletions
diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp
index 5b4694d36..f612e4ade 100644
--- a/source/slang/slang-lower-to-ir.cpp
+++ b/source/slang/slang-lower-to-ir.cpp
@@ -360,6 +360,8 @@ struct SharedIRGenContext
struct IRGenContext
{
+ ASTBuilder* astBuilder;
+
// Shared state for the IR generation process
SharedIRGenContext* shared;
@@ -378,8 +380,9 @@ struct IRGenContext
// might be insufficient.
LoweredValInfo thisVal;
- explicit IRGenContext(SharedIRGenContext* inShared)
+ explicit IRGenContext(SharedIRGenContext* inShared, ASTBuilder* inAstBuilder)
: shared(inShared)
+ , astBuilder(inAstBuilder)
, env(&inShared->globalEnv)
, irBuilder(nullptr)
{}
@@ -969,7 +972,7 @@ static void addLinkageDecoration(
IRInst* inst,
Decl* decl)
{
- String mangledName = getMangledName(decl);
+ String mangledName = getMangledName(context->astBuilder, decl);
if (context->shared->m_obfuscateCode)
{
@@ -1025,7 +1028,7 @@ struct ValLoweringVisitor : ValVisitor<ValLoweringVisitor, LoweredValInfo, Lower
LoweredValInfo visitGenericParamIntVal(GenericParamIntVal* val)
{
return emitDeclRef(context, val->declRef,
- lowerType(context, GetType(val->declRef)));
+ lowerType(context, GetType(context->astBuilder, val->declRef)));
}
LoweredValInfo visitDeclaredSubtypeWitness(DeclaredSubtypeWitness* val)
@@ -1176,14 +1179,14 @@ struct ValLoweringVisitor : ValVisitor<ValLoweringVisitor, LoweredValInfo, Lower
// for emitting the signature of a `CallableDecl`, and we should
// try to re-use that if at all possible.
//
- auto irParamType = lowerType(context, GetType(paramDeclRef));
+ auto irParamType = lowerType(context, GetType(context->astBuilder, paramDeclRef));
auto irParam = subBuilder->emitParam(irParamType);
irParams.add(irParam);
irParamTypes.add(irParamType);
}
- auto irResultType = lowerType(context, GetResultType(callableDeclRef));
+ auto irResultType = lowerType(context, GetResultType(context->astBuilder, callableDeclRef));
auto irFuncType = subBuilder->getFuncType(
irParamTypes,
@@ -1481,7 +1484,7 @@ struct ValLoweringVisitor : ValVisitor<ValLoweringVisitor, LoweredValInfo, Lower
IRType* visitExtractExistentialType(ExtractExistentialType* type)
{
auto declRef = type->declRef;
- auto existentialType = lowerType(context, GetType(declRef));
+ auto existentialType = lowerType(context, GetType(context->astBuilder, declRef));
IRInst* existentialVal = getSimpleVal(context, emitDeclRef(context, declRef, existentialType));
return getBuilder()->emitExtractExistentialType(existentialVal);
}
@@ -1489,7 +1492,7 @@ struct ValLoweringVisitor : ValVisitor<ValLoweringVisitor, LoweredValInfo, Lower
LoweredValInfo visitExtractExistentialSubtypeWitness(ExtractExistentialSubtypeWitness* witness)
{
auto declRef = witness->declRef;
- auto existentialType = lowerType(context, GetType(declRef));
+ auto existentialType = lowerType(context, GetType(context->astBuilder, declRef));
IRInst* existentialVal = getSimpleVal(context, emitDeclRef(context, declRef, existentialType));
return LoweredValInfo::simple(getBuilder()->emitExtractExistentialWitnessTable(existentialVal));
}
@@ -1520,7 +1523,7 @@ struct ValLoweringVisitor : ValVisitor<ValLoweringVisitor, LoweredValInfo, Lower
//
getBuilder()->addExportDecoration(
irType,
- getMangledTypeName(type).getUnownedSlice());
+ getMangledTypeName(context->astBuilder, type).getUnownedSlice());
}
return LoweredValInfo::simple(irType);
}
@@ -1922,7 +1925,7 @@ DeclRef<Decl> createDefaultSpecializedDeclRefImpl(IRGenContext* context, Decl* d
{
DeclRef<Decl> declRef;
declRef.decl = decl;
- declRef.substitutions = createDefaultSubstitutions(context->getSession(), decl);
+ declRef.substitutions = createDefaultSubstitutions(context->astBuilder, decl);
return declRef;
}
//
@@ -1950,11 +1953,11 @@ RefPtr<Type> getThisParamTypeForContainer(
{
if( auto aggTypeDeclRef = parentDeclRef.as<AggTypeDecl>() )
{
- return DeclRefType::Create(context->getSession(), aggTypeDeclRef);
+ return DeclRefType::create(context->astBuilder, aggTypeDeclRef);
}
else if( auto extensionDeclRef = parentDeclRef.as<ExtensionDecl>() )
{
- return GetTargetType(extensionDeclRef);
+ return GetTargetType(context->astBuilder, extensionDeclRef);
}
return nullptr;
@@ -1984,6 +1987,7 @@ struct ExprLoweringVisitorBase : ExprVisitor<Derived, LoweredValInfo>
IRGenContext* context;
IRBuilder* getBuilder() { return context->irBuilder; }
+ ASTBuilder* getASTBuilder() { return context->astBuilder; }
// Lower an expression that should have the same l-value-ness
// as the visitor itself.
@@ -2514,7 +2518,7 @@ struct ExprLoweringVisitorBase : ExprVisitor<Derived, LoweredValInfo>
for (auto paramDeclRef : getMembersOfType<ParamDecl>(funcDeclRef))
{
auto paramDecl = paramDeclRef.getDecl();
- IRType* paramType = lowerType(context, GetType(paramDeclRef));
+ IRType* paramType = lowerType(context, GetType(getASTBuilder(), paramDeclRef));
auto paramDirection = getParameterDirection(paramDecl);
UInt argIndex = argCounter++;
@@ -2529,7 +2533,7 @@ struct ExprLoweringVisitorBase : ExprVisitor<Derived, LoweredValInfo>
// but there are still parameters remaining. This must mean
// that these parameters have default argument expressions
// associated with them.
- argExpr = getInitExpr(paramDeclRef);
+ argExpr = getInitExpr(getASTBuilder(), paramDeclRef);
// Assert that such an expression must have been present.
SLANG_ASSERT(argExpr);
@@ -2879,7 +2883,7 @@ struct ExprLoweringVisitorBase : ExprVisitor<Derived, LoweredValInfo>
LoweredValInfo visitExtractExistentialValueExpr(ExtractExistentialValueExpr* expr)
{
- auto existentialType = lowerType(context, GetType(expr->declRef));
+ auto existentialType = lowerType(context, GetType(getASTBuilder(), expr->declRef));
auto existentialVal = getSimpleVal(context, emitDeclRef(context, expr->declRef, existentialType));
auto openedType = lowerType(context, expr->type);
@@ -4461,9 +4465,7 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo>
}
else
{
- subType = DeclRefType::Create(
- context->getSession(),
- makeDeclRef(parentDecl));
+ subType = DeclRefType::create(context->astBuilder, makeDeclRef(parentDecl));
}
// What is the super-type that we have declared we inherit from?
@@ -4473,7 +4475,7 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo>
// on the type that is conforming, and the type that it conforms to.
//
// TODO: This approach doesn't really make sense for generic `extension` conformances.
- auto mangledName = getMangledNameForConformanceWitness(subType, superType);
+ auto mangledName = getMangledNameForConformanceWitness(context->astBuilder, subType, superType);
// A witness table may need to be generic, if the outer
// declaration (either a type declaration or an `extension`)
@@ -6657,9 +6659,7 @@ static void lowerProgramEntryPointToIR(
// First, lower the entry point like an ordinary function
-
- auto session = context->getSession();
- auto entryPointFuncType = lowerType(context, getFuncType(session, entryPointFuncDeclRef));
+ auto entryPointFuncType = lowerType(context, getFuncType(context->astBuilder, entryPointFuncDeclRef));
auto builder = context->irBuilder;
builder->setInsertInto(builder->getModule()->getModuleInst());
@@ -6669,7 +6669,7 @@ static void lowerProgramEntryPointToIR(
if(!loweredEntryPointFunc->findDecoration<IRLinkageDecoration>())
{
- builder->addExportDecoration(loweredEntryPointFunc, getMangledName(entryPointFuncDeclRef).getUnownedSlice());
+ builder->addExportDecoration(loweredEntryPointFunc, getMangledName(context->astBuilder, entryPointFuncDeclRef).getUnownedSlice());
}
// We may have shader parameters of interface/existential type,
@@ -6725,6 +6725,7 @@ static void ensureAllDeclsRec(
}
IRModule* generateIRForTranslationUnit(
+ ASTBuilder* astBuilder,
TranslationUnitRequest* translationUnit)
{
auto compileRequest = translationUnit->compileRequest;
@@ -6736,7 +6737,7 @@ IRModule* generateIRForTranslationUnit(
translationUnit->getModuleDecl());
SharedIRGenContext* sharedContext = &sharedContextStorage;
- IRGenContext contextStorage(sharedContext);
+ IRGenContext contextStorage(sharedContext, astBuilder);
IRGenContext* context = &contextStorage;
SharedIRBuilder sharedBuilderStorage;
@@ -6926,7 +6927,7 @@ struct SpecializedComponentTypeIRGenContext : ComponentTypeVisitor
);
SharedIRGenContext* sharedContext = &sharedContextStorage;
- IRGenContext contextStorage(sharedContext);
+ IRGenContext contextStorage(sharedContext, linkage->getASTBuilder());
context = &contextStorage;
SharedIRBuilder sharedBuilderStorage;
@@ -7034,8 +7035,8 @@ RefPtr<IRModule> TargetProgram::getOrCreateIRModuleForLayout(DiagnosticSink* sin
/// Specialized IR generation context for when generating IR for layouts.
struct IRLayoutGenContext : IRGenContext
{
- IRLayoutGenContext(SharedIRGenContext* shared)
- : IRGenContext(shared)
+ IRLayoutGenContext(SharedIRGenContext* shared, ASTBuilder* astBuilder)
+ : IRGenContext(shared, astBuilder)
{}
/// Cache for custom key instructions used for entry-point parameter layout information.
@@ -7325,7 +7326,9 @@ RefPtr<IRModule> TargetProgram::createIRModuleForLayout(DiagnosticSink* sink)
linkage->m_obfuscateCode);
auto sharedContext = &sharedContextStorage;
- IRLayoutGenContext contextStorage(sharedContext);
+ ASTBuilder* astBuilder = linkage->getASTBuilder();
+
+ IRLayoutGenContext contextStorage(sharedContext, astBuilder);
auto context = &contextStorage;
SharedIRBuilder sharedBuilderStorage;
@@ -7375,12 +7378,12 @@ RefPtr<IRModule> TargetProgram::createIRModuleForLayout(DiagnosticSink* sink)
if(!funcDeclRef)
continue;
- auto irFuncType = lowerType(context, getFuncType(session, funcDeclRef));
+ auto irFuncType = lowerType(context, getFuncType(astBuilder, funcDeclRef));
auto irFunc = getSimpleVal(context, emitDeclRef(context, funcDeclRef, irFuncType));
if( !irFunc->findDecoration<IRLinkageDecoration>() )
{
- builder->addImportDecoration(irFunc, getMangledName(funcDeclRef).getUnownedSlice());
+ builder->addImportDecoration(irFunc, getMangledName(astBuilder, funcDeclRef).getUnownedSlice());
}
auto irEntryPointLayout = lowerEntryPointLayout(context, entryPointLayout);