From 923ef7af304f2f118b0aee153bd50e054ebc50c9 Mon Sep 17 00:00:00 2001 From: ArielG-NV <159081215+ArielG-NV@users.noreply.github.com> Date: Mon, 22 Apr 2024 10:07:06 -0400 Subject: bit_cast & reinterpret warning if src->dst type not equally sized. (#3988) * bit_cast & reinterpret warning if src->dst type not equally sized. bit_cast & reinterpret warning if src->dst type not equally sized. --------- Co-authored-by: Yong He --- source/slang/slang-ir-lower-bit-cast.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'source/slang/slang-ir-lower-bit-cast.cpp') diff --git a/source/slang/slang-ir-lower-bit-cast.cpp b/source/slang/slang-ir-lower-bit-cast.cpp index 7a9f605d2..33516f54a 100644 --- a/source/slang/slang-ir-lower-bit-cast.cpp +++ b/source/slang/slang-ir-lower-bit-cast.cpp @@ -12,6 +12,7 @@ struct BitCastLoweringContext TargetProgram* targetProgram; IRModule* module; OrderedHashSet workList; + DiagnosticSink* sink; void addToWorkList(IRInst* inst) { @@ -212,8 +213,16 @@ struct BitCastLoweringContext auto operand = inst->getOperand(0); auto fromType = operand->getDataType(); auto toType = inst->getDataType(); + + IRSizeAndAlignment toTypeSize; + getNaturalSizeAndAlignment(targetProgram->getOptionSet(), toType, &toTypeSize); + IRSizeAndAlignment fromTypeSize; + getNaturalSizeAndAlignment(targetProgram->getOptionSet(), fromType, &fromTypeSize); + if (as(fromType) != nullptr && as(toType) != nullptr) { + if (fromTypeSize.size != toTypeSize.size) + sink->diagnose(inst->sourceLoc, Diagnostics::notEqualBitCastSize, fromType, fromTypeSize.size, toType, toTypeSize.size); // Both fromType and toType are basic types, no processing needed. return; } @@ -238,6 +247,10 @@ struct BitCastLoweringContext { return; } + + if (fromTypeSize.size != toTypeSize.size) + sink->diagnose(inst->sourceLoc, Diagnostics::notEqualBitCastSize, fromType, fromTypeSize.size, toType, toTypeSize.size); + // Enumerate all fields in to-type and obtain its value from operand object. IRBuilder builder(module); builder.setInsertBefore(inst); @@ -247,11 +260,12 @@ struct BitCastLoweringContext } }; -void lowerBitCast(TargetProgram* targetProgram, IRModule* module) +void lowerBitCast(TargetProgram* targetProgram, IRModule* module, DiagnosticSink* sink) { BitCastLoweringContext context; context.module = module; context.targetProgram = targetProgram; + context.sink = sink; context.processModule(); } -- cgit v1.2.3