Skip to content

Commit d729dce

Browse files
authored
Merge branch 'main' into hlsl-firstbitlow
2 parents ab3ca85 + 8ce81f1 commit d729dce

File tree

211 files changed

+18958
-12130
lines changed

Some content is hidden

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

211 files changed

+18958
-12130
lines changed

clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,9 @@ void NarrowingConversionsCheck::handleFloatingCast(const ASTContext &Context,
513513
return;
514514
}
515515
const BuiltinType *FromType = getBuiltinType(Rhs);
516-
if (ToType->getKind() < FromType->getKind())
516+
if (!llvm::APFloatBase::isRepresentableBy(
517+
Context.getFloatTypeSemantics(FromType->desugar()),
518+
Context.getFloatTypeSemantics(ToType->desugar())))
517519
diagNarrowType(SourceLoc, Lhs, Rhs);
518520
}
519521
}

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ Changes in existing checks
210210
<clang-tidy/checks/bugprone/forwarding-reference-overload>` check by fixing
211211
a crash when determining if an ``enable_if[_t]`` was found.
212212

213+
- Improve :doc:`bugprone-narrowing-conversions
214+
<clang-tidy/checks/bugprone/narrowing-conversions>` to avoid incorrect check
215+
results when floating point type is not ``float``, ``double`` and
216+
``long double``.
217+
213218
- Improved :doc:`bugprone-optional-value-conversion
214219
<clang-tidy/checks/bugprone/optional-value-conversion>` to support detecting
215220
conversion directly by ``std::make_unique`` and ``std::make_shared``.

clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-narrowingfloatingpoint-option.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ void narrow_double_to_float_not_ok(double d) {
3636
f = narrow_double_to_float_return();
3737
}
3838

39+
float narrow_float16_to_float_return(_Float16 f) {
40+
return f;
41+
}
42+
43+
_Float16 narrow_float_to_float16_return(float f) {
44+
return f;
45+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: narrowing conversion from 'float' to '_Float16' [bugprone-narrowing-conversions]
46+
}
47+
3948
void narrow_fp_constants() {
4049
float f;
4150
f = 0.5; // [dcl.init.list] 7.2 : in-range fp constant to narrower float is not a narrowing.

clang/include/clang/AST/DeclBase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ class DeclListNode {
13341334

13351335
reference operator*() const {
13361336
assert(Ptr && "dereferencing end() iterator");
1337-
if (DeclListNode *CurNode = Ptr.dyn_cast<DeclListNode*>())
1337+
if (DeclListNode *CurNode = dyn_cast<DeclListNode *>(Ptr))
13381338
return CurNode->D;
13391339
return cast<NamedDecl *>(Ptr);
13401340
}
@@ -1344,7 +1344,7 @@ class DeclListNode {
13441344
inline iterator &operator++() { // ++It
13451345
assert(!Ptr.isNull() && "Advancing empty iterator");
13461346

1347-
if (DeclListNode *CurNode = Ptr.dyn_cast<DeclListNode*>())
1347+
if (DeclListNode *CurNode = dyn_cast<DeclListNode *>(Ptr))
13481348
Ptr = CurNode->Rest;
13491349
else
13501350
Ptr = nullptr;

clang/include/clang/Basic/Attr.td

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4353,6 +4353,16 @@ def HLSLLoopHint: StmtAttr {
43534353
let Documentation = [HLSLLoopHintDocs, HLSLUnrollHintDocs];
43544354
}
43554355

4356+
def HLSLControlFlowHint: StmtAttr {
4357+
/// [branch]
4358+
/// [flatten]
4359+
let Spellings = [Microsoft<"branch">, Microsoft<"flatten">];
4360+
let Subjects = SubjectList<[IfStmt],
4361+
ErrorDiag, "'if' statements">;
4362+
let LangOpts = [HLSL];
4363+
let Documentation = [InternalOnly];
4364+
}
4365+
43564366
def CapturedRecord : InheritableAttr {
43574367
// This attribute has no spellings as it is only ever created implicitly.
43584368
let Spellings = [];

clang/lib/Analysis/ThreadSafetyCommon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ til::SExpr *SExprBuilder::translateDeclRefExpr(const DeclRefExpr *DRE,
336336
: (cast<ObjCMethodDecl>(D)->getCanonicalDecl() == Canonical)) {
337337
// Substitute call arguments for references to function parameters
338338
if (const Expr *const *FunArgs =
339-
Ctx->FunArgs.dyn_cast<const Expr *const *>()) {
339+
dyn_cast<const Expr *const *>(Ctx->FunArgs)) {
340340
assert(I < Ctx->NumArgs);
341341
return translate(FunArgs[I], Ctx->Prev);
342342
}

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
#include "llvm/Transforms/Scalar/JumpThreading.h"
8686
#include "llvm/Transforms/Utils/Debugify.h"
8787
#include "llvm/Transforms/Utils/ModuleUtils.h"
88+
#include <limits>
8889
#include <memory>
8990
#include <optional>
9091
using namespace clang;
@@ -119,6 +120,9 @@ static cl::opt<PGOOptions::ColdFuncOpt> ClPGOColdFuncAttr(
119120

120121
extern cl::opt<InstrProfCorrelator::ProfCorrelatorKind> ProfileCorrelate;
121122
} // namespace llvm
123+
namespace clang {
124+
extern llvm::cl::opt<bool> ClSanitizeGuardChecks;
125+
}
122126

123127
namespace {
124128

@@ -1023,6 +1027,14 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
10231027
PB.registerScalarOptimizerLateEPCallback([this](FunctionPassManager &FPM,
10241028
OptimizationLevel Level) {
10251029
BoundsCheckingPass::Options Options;
1030+
if (CodeGenOpts.SanitizeSkipHotCutoffs[SanitizerKind::SO_LocalBounds] ||
1031+
ClSanitizeGuardChecks) {
1032+
static_assert(SanitizerKind::SO_LocalBounds <=
1033+
std::numeric_limits<
1034+
decltype(Options.GuardKind)::value_type>::max(),
1035+
"Update type of llvm.allow.ubsan.check.");
1036+
Options.GuardKind = SanitizerKind::SO_LocalBounds;
1037+
}
10261038
Options.Merge =
10271039
CodeGenOpts.SanitizeMergeHandlers.has(SanitizerKind::LocalBounds);
10281040
if (!CodeGenOpts.SanitizeTrap.has(SanitizerKind::LocalBounds)) {

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,13 @@
5252
using namespace clang;
5353
using namespace CodeGen;
5454

55+
namespace clang {
5556
// TODO: Introduce frontend options to enabled per sanitizers, similar to
5657
// `fsanitize-trap`.
57-
static llvm::cl::opt<bool> ClSanitizeGuardChecks(
58+
llvm::cl::opt<bool> ClSanitizeGuardChecks(
5859
"ubsan-guard-checks", llvm::cl::Optional,
5960
llvm::cl::desc("Guard UBSAN checks with `llvm.allow.ubsan.check()`."));
61+
} // namespace clang
6062

6163
//===--------------------------------------------------------------------===//
6264
// Defines for metadata

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,8 @@ void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) {
760760
bool noinline = false;
761761
bool alwaysinline = false;
762762
bool noconvergent = false;
763+
HLSLControlFlowHintAttr::Spelling flattenOrBranch =
764+
HLSLControlFlowHintAttr::SpellingNotCalculated;
763765
const CallExpr *musttail = nullptr;
764766

765767
for (const auto *A : S.getAttrs()) {
@@ -791,13 +793,17 @@ void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) {
791793
Builder.CreateAssumption(AssumptionVal);
792794
}
793795
} break;
796+
case attr::HLSLControlFlowHint: {
797+
flattenOrBranch = cast<HLSLControlFlowHintAttr>(A)->getSemanticSpelling();
798+
} break;
794799
}
795800
}
796801
SaveAndRestore save_nomerge(InNoMergeAttributedStmt, nomerge);
797802
SaveAndRestore save_noinline(InNoInlineAttributedStmt, noinline);
798803
SaveAndRestore save_alwaysinline(InAlwaysInlineAttributedStmt, alwaysinline);
799804
SaveAndRestore save_noconvergent(InNoConvergentAttributedStmt, noconvergent);
800805
SaveAndRestore save_musttail(MustTailCall, musttail);
806+
SaveAndRestore save_flattenOrBranch(HLSLControlFlowAttr, flattenOrBranch);
801807
EmitStmt(S.getSubStmt(), S.getAttrs());
802808
}
803809

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "llvm/IR/DataLayout.h"
4141
#include "llvm/IR/Dominators.h"
4242
#include "llvm/IR/FPEnv.h"
43+
#include "llvm/IR/Instruction.h"
4344
#include "llvm/IR/IntrinsicInst.h"
4445
#include "llvm/IR/Intrinsics.h"
4546
#include "llvm/IR/MDBuilder.h"
@@ -2086,7 +2087,30 @@ void CodeGenFunction::EmitBranchOnBoolExpr(
20862087
Weights = createProfileWeights(TrueCount, CurrentCount - TrueCount);
20872088
}
20882089

2089-
Builder.CreateCondBr(CondV, TrueBlock, FalseBlock, Weights, Unpredictable);
2090+
llvm::Instruction *BrInst = Builder.CreateCondBr(CondV, TrueBlock, FalseBlock,
2091+
Weights, Unpredictable);
2092+
switch (HLSLControlFlowAttr) {
2093+
case HLSLControlFlowHintAttr::Microsoft_branch:
2094+
case HLSLControlFlowHintAttr::Microsoft_flatten: {
2095+
llvm::MDBuilder MDHelper(CGM.getLLVMContext());
2096+
2097+
llvm::ConstantInt *BranchHintConstant =
2098+
HLSLControlFlowAttr ==
2099+
HLSLControlFlowHintAttr::Spelling::Microsoft_branch
2100+
? llvm::ConstantInt::get(CGM.Int32Ty, 1)
2101+
: llvm::ConstantInt::get(CGM.Int32Ty, 2);
2102+
2103+
SmallVector<llvm::Metadata *, 2> Vals(
2104+
{MDHelper.createString("hlsl.controlflow.hint"),
2105+
MDHelper.createConstant(BranchHintConstant)});
2106+
BrInst->setMetadata("hlsl.controlflow.hint",
2107+
llvm::MDNode::get(CGM.getLLVMContext(), Vals));
2108+
break;
2109+
}
2110+
// This is required to avoid warnings during compilation
2111+
case HLSLControlFlowHintAttr::SpellingNotCalculated:
2112+
break;
2113+
}
20902114
}
20912115

20922116
/// ErrorUnsupported - Print out an error that codegen doesn't support the

0 commit comments

Comments
 (0)