diff options
| author | Jay Kwak <82421531+jkwak-work@users.noreply.github.com> | 2024-06-04 10:26:12 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-04 10:26:12 -0700 |
| commit | 9b6b31be5b38c588bde28d5f215d78cfc62620da (patch) | |
| tree | 03330e5f920c7f7c45375ddbac8ff449cbbcbe01 /source | |
| parent | 89c1fd0dd1581221f583653a9dfa6d1cf990577c (diff) | |
Print warning when operator<< shifting too much (#4255)
* Print warning when operator<< shifting too much
Closes #3944
For the given type of the left side operand to `operator<<` is not big
enough for the right side operand, print a warning that the result will
be always zero.
Diffstat (limited to 'source')
| -rw-r--r-- | source/slang/slang-diagnostic-defs.h | 2 | ||||
| -rw-r--r-- | source/slang/slang-ir-operator-shift-overflow.cpp | 70 | ||||
| -rw-r--r-- | source/slang/slang-ir-operator-shift-overflow.h | 15 | ||||
| -rw-r--r-- | source/slang/slang-lower-to-ir.cpp | 5 |
4 files changed, 91 insertions, 1 deletions
diff --git a/source/slang/slang-diagnostic-defs.h b/source/slang/slang-diagnostic-defs.h index 7385d5e33..4cf6a899d 100644 --- a/source/slang/slang-diagnostic-defs.h +++ b/source/slang/slang-diagnostic-defs.h @@ -740,6 +740,8 @@ DIAGNOSTIC(41021, Error, differentiableFuncMustHaveOutput, "a differentiable fun DIAGNOSTIC(41022, Error, differentiableFuncMustHaveInput, "a differentiable function must have at least one differentiable input.") DIAGNOSTIC(41023, Error, getStringHashMustBeOnStringLiteral, "getStringHash can only be called when argument is statically resolvable to a string literal") +DIAGNOSTIC(41030, Warning, operatorShiftLeftOverflow, "left shift amount exceeds the number of bits and the result will be always zero, (`$0` << `$1`).") + DIAGNOSTIC(41901, Error, unsupportedUseOfLValueForAutoDiff, "unsupported use of L-value for auto differentiation.") DIAGNOSTIC(41902, Error, cannotDifferentiateDynamicallyIndexedData, "cannot auto-differentiate mixed read/write access to dynamically indexed data in '$0'.") diff --git a/source/slang/slang-ir-operator-shift-overflow.cpp b/source/slang/slang-ir-operator-shift-overflow.cpp new file mode 100644 index 000000000..33ee53262 --- /dev/null +++ b/source/slang/slang-ir-operator-shift-overflow.cpp @@ -0,0 +1,70 @@ +// slang-ir-operator-shift-overflow.cpp +#include "slang-ir-operator-shift-overflow.h" + +#include "../../slang.h" +#include "slang-ir.h" +#include "slang-ir-insts.h" +#include "slang-ir-layout.h" + +namespace Slang { + + class DiagnosticSink; + struct IRModule; + + void checkForOperatorShiftOverflowRecursive( + IRInst* inst, + CompilerOptionSet& optionSet, + DiagnosticSink* sink) + { + if (auto code = as<IRGlobalValueWithCode>(inst)) + { + for (auto block : code->getBlocks()) + { + for (auto opInst : block->getChildren()) + { + switch (opInst->getOp()) + { + case kIROp_Lsh: + { + SLANG_ASSERT(opInst->getOperandCount() == 2); + + IRInst* rhs = opInst->getOperand(1); + auto rhsLit = as<IRIntLit>(rhs); + if (!rhsLit) + continue; + + IRInst* lhs = opInst->getOperand(0); + IRType* lhsType = lhs->getDataType(); + + IRSizeAndAlignment sizeAlignment; + if (SLANG_FAILED(getNaturalSizeAndAlignment(optionSet, lhsType, &sizeAlignment))) + continue; + + IRIntegerValue shiftAmount = rhsLit->getValue(); + if (sizeAlignment.size * 8 <= shiftAmount) + { + sink->diagnose(opInst, Diagnostics::operatorShiftLeftOverflow, lhsType, shiftAmount); + } + break; + } + } + } + } + } + + for (auto childInst : inst->getChildren()) + { + checkForOperatorShiftOverflowRecursive(childInst, optionSet, sink); + } + } + + void checkForOperatorShiftOverflow( + IRModule* module, + CompilerOptionSet& optionSet, + DiagnosticSink* sink) + { + // Look for `operator<<` instructions + checkForOperatorShiftOverflowRecursive(module->getModuleInst(), optionSet, sink); + } + +} diff --git a/source/slang/slang-ir-operator-shift-overflow.h b/source/slang/slang-ir-operator-shift-overflow.h new file mode 100644 index 000000000..59280d82c --- /dev/null +++ b/source/slang/slang-ir-operator-shift-overflow.h @@ -0,0 +1,15 @@ +// slang-ir-operator-shift-overflow.h +#pragma once + +#include "slang-compiler-options.h" + +namespace Slang +{ + class DiagnosticSink; + struct IRModule; + + void checkForOperatorShiftOverflow( + IRModule* module, + CompilerOptionSet& optionSet, + DiagnosticSink* sink); +} diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index 5c4196839..bfc4c444d 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -23,6 +23,7 @@ #include "slang-ir-check-differentiability.h" #include "slang-ir-check-recursive-type.h" #include "slang-ir-missing-return.h" +#include "slang-ir-operator-shift-overflow.h" #include "slang-ir-sccp.h" #include "slang-ir-ssa.h" #include "slang-ir-strip.h" @@ -10917,13 +10918,13 @@ RefPtr<IRModule> generateIRForTranslationUnit( break; } - // Check for using uninitialized out parameters. if (compileRequest->getLinkage()->m_optionSet.shouldRunNonEssentialValidation()) { // Propagate `constexpr`-ness through the dataflow graph (and the // call graph) based on constraints imposed by different instructions. propagateConstExpr(module, compileRequest->getSink()); + // Check for using uninitialized out parameters. checkForUsingUninitializedOutParams(module, compileRequest->getSink()); // TODO: give error messages if any `undefined` or @@ -10936,6 +10937,8 @@ RefPtr<IRModule> generateIRForTranslationUnit( // Check for invalid differentiable function body. checkAutoDiffUsages(module, compileRequest->getSink()); + + checkForOperatorShiftOverflow(module, linkage->m_optionSet, compileRequest->getSink()); } // The "mandatory" optimization passes may make use of the |
