Skip to content

Commit 7ad9c9c

Browse files
author
Greg Roth
authored
Fallthrough (#4843)
A common source of bugs. Although many of these were harmless, some were not. By eliminating them and enabling this warning as an error, we won't add more in the future. Fixed a real SPIRV bug that required a test update to expect the correct behavior. SPIRV testing was expecting incorrect results that came from a fallthrough error
1 parent 4d66ec8 commit 7ad9c9c

130 files changed

Lines changed: 391 additions & 243 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmake/modules/HandleLLVMOptions.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ if( MSVC )
379379

380380
elseif( LLVM_COMPILER_IS_GCC_COMPATIBLE )
381381
if (LLVM_ENABLE_WARNINGS)
382-
append("-Wall -W -Wno-unused-parameter -Wwrite-strings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
382+
append("-Wall -W -Wno-unused-parameter -Wwrite-strings -Wimplicit-fallthrough" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
383383
append("-Wcast-qual" CMAKE_CXX_FLAGS)
384384

385385
# Disable unknown pragma warnings because the output is just too long with them.

external/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ if (${ENABLE_SPIRV_CODEGEN})
3939

4040
if (NOT TARGET SPIRV-Tools)
4141
if (IS_DIRECTORY ${DXC_SPIRV_TOOLS_DIR})
42+
# Avoid implicit fallthrough warning from clang
43+
# This add_compile_options() will only affect the current directory and its subdirectories.
44+
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
45+
add_compile_options(-Wno-implicit-fallthrough)
46+
endif(NOT WIN32)
4247
# We only need the library from SPIRV-Tools.
4348
set(SPIRV_SKIP_EXECUTABLES ON CACHE BOOL "Skip building SPIRV-Tools executables")
4449
if (NOT HLSL_ENABLE_DEBUG_ITERATORS)

include/dxc/DXIL/DxilSigPoint.inl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,13 @@ DXIL::SigPointKind SigPoint::GetKind(DXIL::ShaderKind shaderKind, DXIL::Signatur
166166
if (isSpecialInput) {
167167
switch (shaderKind) {
168168
case DXIL::ShaderKind::Hull:
169-
if (sigKind == DXIL::SignatureKind::Input)
170-
return isPatchConstantFunction ? DXIL::SigPointKind::PCIn : DXIL::SigPointKind::HSIn;
169+
if (sigKind == DXIL::SignatureKind::Input)
170+
return isPatchConstantFunction ? DXIL::SigPointKind::PCIn : DXIL::SigPointKind::HSIn;
171+
break;
171172
case DXIL::ShaderKind::Geometry:
172173
if (sigKind == DXIL::SignatureKind::Input)
173174
return DXIL::SigPointKind::GSIn;
175+
break;
174176
default:
175177
break;
176178
}

include/dxc/Support/WinAdapter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@
312312

313313
#define _Printf_format_string_
314314
#define _Null_terminated_
315-
#define __fallthrough
316315

317316
#define _Field_size_(size)
318317
#define _Field_size_full_(size)

include/llvm/Support/Compiler.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,17 @@
220220
#define LLVM_ATTRIBUTE_RETURNS_NOALIAS
221221
#endif
222222

223+
#if __cplusplus > 201402L
224+
#define LLVM_FALLTHROUGH [[fallthrough]]
225+
#elif defined(__clang__)
226+
#define LLVM_FALLTHROUGH [[clang::fallthrough]]
227+
#elif defined(_MSC_VER)
228+
#define LLVM_FALLTHROUGH __fallthrough
229+
#else
230+
#define LLVM_FALLTHROUGH [[gnu::fallthrough]]
231+
#endif
232+
233+
223234
/// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
224235
/// pedantic diagnostics.
225236
#ifdef __GNUC__

lib/Analysis/BasicAliasAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ static Value *GetLinearExpression(Value *V, APInt &Scale, APInt &Offset,
216216
if (!MaskedValueIsZero(BOp->getOperand(0), RHSC->getValue(), DL, 0, AC,
217217
BOp, DT))
218218
break;
219-
// FALL THROUGH.
219+
LLVM_FALLTHROUGH; // HLSL Change
220220
case Instruction::Add:
221221
V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, Extension,
222222
DL, Depth + 1, AC, DT);

lib/Analysis/InstructionSimplify.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2721,7 +2721,7 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
27212721
Q.CxtI, Q.DT);
27222722
if (!KnownNonNegative)
27232723
break;
2724-
// fall-through
2724+
LLVM_FALLTHROUGH; // HLSL Change
27252725
case ICmpInst::ICMP_EQ:
27262726
case ICmpInst::ICMP_UGT:
27272727
case ICmpInst::ICMP_UGE:
@@ -2732,7 +2732,7 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
27322732
Q.CxtI, Q.DT);
27332733
if (!KnownNonNegative)
27342734
break;
2735-
// fall-through
2735+
LLVM_FALLTHROUGH; // HLSL Change
27362736
case ICmpInst::ICMP_NE:
27372737
case ICmpInst::ICMP_ULT:
27382738
case ICmpInst::ICMP_ULE:
@@ -2752,7 +2752,7 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
27522752
Q.CxtI, Q.DT);
27532753
if (!KnownNonNegative)
27542754
break;
2755-
// fall-through
2755+
LLVM_FALLTHROUGH; // HLSL Change
27562756
case ICmpInst::ICMP_NE:
27572757
case ICmpInst::ICMP_UGT:
27582758
case ICmpInst::ICMP_UGE:
@@ -2763,7 +2763,7 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
27632763
Q.CxtI, Q.DT);
27642764
if (!KnownNonNegative)
27652765
break;
2766-
// fall-through
2766+
LLVM_FALLTHROUGH; // HLSL Change
27672767
case ICmpInst::ICMP_EQ:
27682768
case ICmpInst::ICMP_ULT:
27692769
case ICmpInst::ICMP_ULE:
@@ -2823,7 +2823,7 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
28232823
case Instruction::LShr:
28242824
if (ICmpInst::isSigned(Pred))
28252825
break;
2826-
// fall-through
2826+
LLVM_FALLTHROUGH; // HLSL Change
28272827
case Instruction::SDiv:
28282828
case Instruction::AShr:
28292829
if (!LBO->isExact() || !RBO->isExact())

lib/Analysis/MemoryDependenceAnalysis.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ MemDepResult MemoryDependenceAnalysis::getPointerDependencyFrom(
641641
// load query, we can safely ignore it (scan past it).
642642
if (isLoad)
643643
continue;
644+
LLVM_FALLTHROUGH; // HLSL Change
644645
default:
645646
// Otherwise, there is a potential dependence. Return a clobber.
646647
return MemDepResult::getClobber(Inst);
@@ -1014,6 +1015,7 @@ SortNonLocalDepInfoCache(MemoryDependenceAnalysis::NonLocalDepInfo &Cache,
10141015
std::upper_bound(Cache.begin(), Cache.end()-1, Val);
10151016
Cache.insert(Entry, Val);
10161017
// FALL THROUGH.
1018+
LLVM_FALLTHROUGH; // HLSL Change
10171019
}
10181020
case 1:
10191021
// One new entry, Just insert the new value at the appropriate position.

lib/Analysis/ScalarEvolution.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4380,7 +4380,7 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) {
43804380
case ICmpInst::ICMP_SLT:
43814381
case ICmpInst::ICMP_SLE:
43824382
std::swap(LHS, RHS);
4383-
// fall through
4383+
LLVM_FALLTHROUGH; // HLSL Change
43844384
case ICmpInst::ICMP_SGT:
43854385
case ICmpInst::ICMP_SGE:
43864386
// a >s b ? a+x : b+x -> smax(a, b)+x
@@ -4404,7 +4404,7 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) {
44044404
case ICmpInst::ICMP_ULT:
44054405
case ICmpInst::ICMP_ULE:
44064406
std::swap(LHS, RHS);
4407-
// fall through
4407+
LLVM_FALLTHROUGH; // HLSL Change
44084408
case ICmpInst::ICMP_UGT:
44094409
case ICmpInst::ICMP_UGE:
44104410
// a >u b ? a+x : b+x -> umax(a, b)+x
@@ -4458,7 +4458,7 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) {
44584458
default:
44594459
break;
44604460
}
4461-
}
4461+
} break;
44624462

44634463
default: // We cannot analyze this expression.
44644464
break;
@@ -6696,6 +6696,7 @@ ScalarEvolution::isKnownPredicateWithRanges(ICmpInst::Predicate Pred,
66966696
llvm_unreachable("Unexpected ICmpInst::Predicate value!");
66976697
case ICmpInst::ICMP_SGT:
66986698
std::swap(LHS, RHS);
6699+
LLVM_FALLTHROUGH; // HLSL Change
66996700
case ICmpInst::ICMP_SLT: {
67006701
ConstantRange LHSRange = getSignedRange(LHS);
67016702
ConstantRange RHSRange = getSignedRange(RHS);
@@ -6707,6 +6708,7 @@ ScalarEvolution::isKnownPredicateWithRanges(ICmpInst::Predicate Pred,
67076708
}
67086709
case ICmpInst::ICMP_SGE:
67096710
std::swap(LHS, RHS);
6711+
LLVM_FALLTHROUGH; // HLSL Change
67106712
case ICmpInst::ICMP_SLE: {
67116713
ConstantRange LHSRange = getSignedRange(LHS);
67126714
ConstantRange RHSRange = getSignedRange(RHS);
@@ -6718,6 +6720,7 @@ ScalarEvolution::isKnownPredicateWithRanges(ICmpInst::Predicate Pred,
67186720
}
67196721
case ICmpInst::ICMP_UGT:
67206722
std::swap(LHS, RHS);
6723+
LLVM_FALLTHROUGH; // HLSL Change
67216724
case ICmpInst::ICMP_ULT: {
67226725
ConstantRange LHSRange = getUnsignedRange(LHS);
67236726
ConstantRange RHSRange = getUnsignedRange(RHS);
@@ -6729,6 +6732,7 @@ ScalarEvolution::isKnownPredicateWithRanges(ICmpInst::Predicate Pred,
67296732
}
67306733
case ICmpInst::ICMP_UGE:
67316734
std::swap(LHS, RHS);
6735+
LLVM_FALLTHROUGH; // HLSL Change
67326736
case ICmpInst::ICMP_ULE: {
67336737
ConstantRange LHSRange = getUnsignedRange(LHS);
67346738
ConstantRange RHSRange = getUnsignedRange(RHS);
@@ -7052,6 +7056,7 @@ bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred,
70527056
if (isImpliedCondOperands(Pred, LHS, RHS, V,
70537057
getConstant(SharperMin)))
70547058
return true;
7059+
LLVM_FALLTHROUGH; // HLSL Change
70557060

70567061
case ICmpInst::ICMP_SGT:
70577062
case ICmpInst::ICMP_UGT:
@@ -7066,7 +7071,7 @@ bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred,
70667071

70677072
if (isImpliedCondOperands(Pred, LHS, RHS, V, getConstant(Min)))
70687073
return true;
7069-
7074+
break;
70707075
default:
70717076
// No change
70727077
break;
@@ -7163,7 +7168,7 @@ static bool IsKnownPredicateViaMinOrMax(ScalarEvolution &SE,
71637168

71647169
case ICmpInst::ICMP_SGE:
71657170
std::swap(LHS, RHS);
7166-
// fall through
7171+
LLVM_FALLTHROUGH; // HLSL Change
71677172
case ICmpInst::ICMP_SLE:
71687173
return
71697174
// min(A, ...) <= A
@@ -7173,7 +7178,7 @@ static bool IsKnownPredicateViaMinOrMax(ScalarEvolution &SE,
71737178

71747179
case ICmpInst::ICMP_UGE:
71757180
std::swap(LHS, RHS);
7176-
// fall through
7181+
LLVM_FALLTHROUGH; // HLSL Change
71777182
case ICmpInst::ICMP_ULE:
71787183
return
71797184
// min(A, ...) <= A
@@ -8423,6 +8428,7 @@ ScalarEvolution::computeBlockDisposition(const SCEV *S, const BasicBlock *BB) {
84238428
return DoesNotDominateBlock;
84248429
}
84258430
// FALL THROUGH into SCEVNAryExpr handling.
8431+
LLVM_FALLTHROUGH; // HLSL Change
84268432
case scAddExpr:
84278433
case scMulExpr:
84288434
case scUMaxExpr:

lib/Analysis/ValueTracking.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2182,6 +2182,7 @@ bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
21822182
case Instruction::SExt:
21832183
if (!LookThroughSExt) return false;
21842184
// otherwise fall through to ZExt
2185+
LLVM_FALLTHROUGH; // HLSL Change
21852186
case Instruction::ZExt:
21862187
return ComputeMultiple(I->getOperand(0), Base, Multiple,
21872188
LookThroughSExt, Depth+1);
@@ -2331,7 +2332,7 @@ bool llvm::CannotBeOrderedLessThanZero(const Value *V, unsigned Depth) {
23312332
// x*x is always non-negative or a NaN.
23322333
if (I->getOperand(0) == I->getOperand(1))
23332334
return true;
2334-
// Fall through
2335+
LLVM_FALLTHROUGH; // HLSL Change
23352336
case Instruction::FAdd:
23362337
case Instruction::FDiv:
23372338
case Instruction::FRem:

0 commit comments

Comments
 (0)