From 57b09a8986668626c37055e431fa0ac6449d7214 Mon Sep 17 00:00:00 2001 From: Jay Kwak <82421531+jkwak-work@users.noreply.github.com> Date: Fri, 7 Feb 2025 18:27:23 -0800 Subject: Use and() and or() functions for logical-AND and OR (#6310) * Use and() and or() functions for logical-AND and OR With this commit, Slang will emit function calls to `and()` and `or()` for the logical-AND and logical-OR when the operands are non-scalar and the target profile is SM6.0 and above. This is required change from SM6.0. For WGSL, there is no operator overloadings of `&&` and `||` when the operands are non-scalar. Unlike HLSL, WGSL also don't have `and()` nor `or()`. Alternatively, we can use `select()`. --- source/slang/slang-emit-hlsl.cpp | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'source/slang/slang-emit-hlsl.cpp') diff --git a/source/slang/slang-emit-hlsl.cpp b/source/slang/slang-emit-hlsl.cpp index 9ebec0893..ff4514d69 100644 --- a/source/slang/slang-emit-hlsl.cpp +++ b/source/slang/slang-emit-hlsl.cpp @@ -821,6 +821,53 @@ bool HLSLSourceEmitter::tryEmitInstExprImpl(IRInst* inst, const EmitOpInfo& inOu } break; } + case kIROp_And: + case kIROp_Or: + { + // SM6.0 requires to use `and()` and `or()` functions for the logical-AND and + // logical-OR, respectively, with non-scalar operands. + auto targetProfile = getTargetProgram()->getOptionSet().getProfile(); + if (targetProfile.getVersion() < ProfileVersion::DX_6_0) + return false; + + if (as(inst->getDataType())) + return false; + + if (inst->getOp() == kIROp_And) + { + m_writer->emit("and("); + } + else + { + m_writer->emit("or("); + } + emitOperand(inst->getOperand(0), getInfo(EmitOp::General)); + m_writer->emit(", "); + emitOperand(inst->getOperand(1), getInfo(EmitOp::General)); + m_writer->emit(")"); + return true; + } + case kIROp_Select: + { + // SM6.0 requires to use `select()` instead of the ternary operator "?:" when the + // operands are non-scalar. + auto targetProfile = getTargetProgram()->getOptionSet().getProfile(); + if (targetProfile.getVersion() < ProfileVersion::DX_6_0) + return false; + + if (as(inst->getDataType())) + return false; + + m_writer->emit("select("); + emitOperand(inst->getOperand(0), getInfo(EmitOp::General)); + m_writer->emit(", "); + emitOperand(inst->getOperand(1), getInfo(EmitOp::General)); + m_writer->emit(", "); + emitOperand(inst->getOperand(2), getInfo(EmitOp::General)); + m_writer->emit(")"); + return true; + } + case kIROp_BitCast: { // For simplicity, we will handle all bit-cast operations -- cgit v1.2.3