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-ir-legalize-binary-operator.cpp | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) (limited to 'source/slang/slang-ir-legalize-binary-operator.cpp') diff --git a/source/slang/slang-ir-legalize-binary-operator.cpp b/source/slang/slang-ir-legalize-binary-operator.cpp index a1affb7e9..1595aa130 100644 --- a/source/slang/slang-ir-legalize-binary-operator.cpp +++ b/source/slang/slang-ir-legalize-binary-operator.cpp @@ -118,4 +118,101 @@ void legalizeBinaryOp(IRInst* inst) } } +void legalizeLogicalAndOr(IRInst* inst) +{ + switch (inst->getOp()) + { + case kIROp_And: + case kIROp_Or: + { + IRBuilder builder(inst); + builder.setInsertBefore(inst); + + // Logical-AND and logical-OR takes boolean types as its operands. + // If they are not, legalize them by casting to boolean type. + // + SLANG_ASSERT(inst->getOperandCount() == 2); + for (UInt i = 0; i < 2; i++) + { + auto operand = inst->getOperand(i); + auto operandDataType = operand->getDataType(); + + if (auto vecType = as(operandDataType)) + { + if (!as(vecType->getElementType())) + { + // Cast operand to vector + auto elemCount = vecType->getElementCount(); + auto vb = builder.getVectorType(builder.getBoolType(), elemCount); + auto v = builder.emitCast(vb, operand); + builder.replaceOperand(inst->getOperands() + i, v); + } + } + else if (!as(operandDataType)) + { + // Cast operand to bool + auto s = builder.emitCast(builder.getBoolType(), operand); + builder.replaceOperand(inst->getOperands() + i, s); + } + } + + // Legalize the return type; mostly for SPIRV. + // The return type of OpLogicalOr must be boolean type. + // If not, we need to recreate the instruction with boolean return type. + // Then, we have to cast it back to the original type so that other instrucitons that + // use have the matching types. + // + auto dataType = inst->getDataType(); + auto lhs = inst->getOperand(0); + auto rhs = inst->getOperand(1); + IRInst* newInst = nullptr; + + if (auto vecType = as(dataType)) + { + if (!as(vecType->getElementType())) + { + // Return type should be vector + auto elemCount = vecType->getElementCount(); + auto vb = builder.getVectorType(builder.getBoolType(), elemCount); + + if (inst->getOp() == kIROp_And) + { + newInst = builder.emitAnd(vb, lhs, rhs); + } + else + { + newInst = builder.emitOr(vb, lhs, rhs); + } + newInst = builder.emitCast(dataType, newInst); + } + } + else if (!as(dataType)) + { + // Return type should be bool + if (inst->getOp() == kIROp_And) + { + newInst = builder.emitAnd(builder.getBoolType(), lhs, rhs); + } + else + { + newInst = builder.emitOr(builder.getBoolType(), lhs, rhs); + } + newInst = builder.emitCast(dataType, newInst); + } + + if (newInst && inst != newInst) + { + inst->replaceUsesWith(newInst); + inst->removeAndDeallocate(); + } + } + break; + } + + for (auto child : inst->getModifiableChildren()) + { + legalizeLogicalAndOr(child); + } +} + } // namespace Slang -- cgit v1.2.3