From 72016f9201e4d7820f62e7ef78cee98ed1fc4da0 Mon Sep 17 00:00:00 2001 From: Jay Kwak <82421531+jkwak-work@users.noreply.github.com> Date: Mon, 10 Jun 2024 13:29:02 -0700 Subject: Partial implementation of static_assert (#4294) * Error out for types not supported by texture sample functions This commit prints errors with a new keyword, `static_assert`, when the given texture type is not supported for the target. * Moving the check to linkAndOptimizeIR after specialization is done * Remove unnecessary change * Adding test * Remove kIROp_StaticAssert once processed * Do not remove StaticAssert because it is needed for the next specialization * Remove after iteration of child is done --------- Co-authored-by: Yong He --- source/slang/slang-emit.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'source/slang/slang-emit.cpp') diff --git a/source/slang/slang-emit.cpp b/source/slang/slang-emit.cpp index 02dab6dfa..da348fa64 100644 --- a/source/slang/slang-emit.cpp +++ b/source/slang/slang-emit.cpp @@ -362,6 +362,51 @@ void calcRequiredLoweringPassSet(RequiredLoweringPassSet& result, CodeGenContext } } +bool checkStaticAssert(IRInst* inst, DiagnosticSink* sink) +{ + switch (inst->getOp()) + { + case kIROp_StaticAssert: + { + IRInst* condi = inst->getOperand(0); + if (auto condiLit = as(condi)) + { + if (!condiLit->getValue()) + { + IRInst* msg = inst->getOperand(1); + if (auto msgLit = as(msg)) + { + sink->diagnose(inst, Diagnostics::staticAssertionFailure, msgLit->getStringSlice()); + } + else + { + sink->diagnose(inst, Diagnostics::staticAssertionFailureWithoutMessage); + } + } + } + else + { + sink->diagnose(condi, Diagnostics::staticAssertionConditionNotConstant); + } + + return true; + } + } + + List removeList; + for (auto child : inst->getChildren()) + { + if (checkStaticAssert(child, sink)) + removeList.add(child); + } + for (auto child : removeList) + { + child->removeAndDeallocate(); + } + + return false; +} + Result linkAndOptimizeIR( CodeGenContext* codeGenContext, LinkingAndOptimizationOptions const& options, @@ -881,6 +926,10 @@ Result linkAndOptimizeIR( validateIRModuleIfEnabled(codeGenContext, irModule); + // Process `static_assert` after the specialization is done. + // Some information for `static_assert` is available only after the specialization. + checkStaticAssert(irModule->getModuleInst(), sink); + // For HLSL (and fxc/dxc) only, we need to "wrap" any // structured buffers defined over matrix types so // that they instead use an intermediate `struct`. -- cgit v1.2.3