Skip to content

Commit 8bbc653

Browse files
committed
merge main into amd-staging
2 parents ba04503 + 5bc1667 commit 8bbc653

File tree

101 files changed

+1511
-404
lines changed

Some content is hidden

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

101 files changed

+1511
-404
lines changed

clang/include/clang/Driver/Action.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ class Action {
7676
LinkerWrapperJobClass,
7777
StaticLibJobClass,
7878
BinaryAnalyzeJobClass,
79+
BinaryTranslatorJobClass,
7980

8081
JobClassFirst = PreprocessJobClass,
81-
JobClassLast = BinaryAnalyzeJobClass
82+
JobClassLast = BinaryTranslatorJobClass
8283
};
8384

8485
// The offloading kind determines if this action is binded to a particular
@@ -687,6 +688,17 @@ class BinaryAnalyzeJobAction : public JobAction {
687688
}
688689
};
689690

691+
class BinaryTranslatorJobAction : public JobAction {
692+
void anchor() override;
693+
694+
public:
695+
BinaryTranslatorJobAction(Action *Input, types::ID Type);
696+
697+
static bool classof(const Action *A) {
698+
return A->getKind() == BinaryTranslatorJobClass;
699+
}
700+
};
701+
690702
} // namespace driver
691703
} // namespace clang
692704

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9354,6 +9354,7 @@ def : Option<["/", "-"], "Qembed_debug", KIND_FLAG>, Group<dxc_Group>,
93549354
HelpText<"Embed PDB in shader container (ignored)">;
93559355
def spirv : DXCFlag<"spirv">,
93569356
HelpText<"Generate SPIR-V code">;
9357+
def metal : DXCFlag<"metal">, HelpText<"Generate Metal library">;
93579358
def fspv_target_env_EQ : Joined<["-"], "fspv-target-env=">, Group<dxc_Group>,
93589359
HelpText<"Specify the target environment">,
93599360
Values<"vulkan1.2, vulkan1.3">;

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 111 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "clang/Analysis/Analyses/UnsafeBufferUsage.h"
10+
#include "clang/AST/APValue.h"
1011
#include "clang/AST/ASTContext.h"
12+
#include "clang/AST/Attr.h"
1113
#include "clang/AST/Decl.h"
1214
#include "clang/AST/DynamicRecursiveASTVisitor.h"
1315
#include "clang/AST/Expr.h"
@@ -353,23 +355,90 @@ isInUnspecifiedUntypedContext(internal::Matcher<Stmt> InnerMatcher) {
353355
return stmt(anyOf(CompStmt, IfStmtThen, IfStmtElse));
354356
}
355357

358+
// Returns true iff integer E1 is equivalent to integer E2.
359+
//
360+
// For now we only support such expressions:
361+
// expr := DRE | const-value | expr BO expr
362+
// BO := '*' | '+'
363+
//
364+
// FIXME: We can reuse the expression comparator of the interop analysis after
365+
// it has been upstreamed.
366+
static bool areEqualIntegers(const Expr *E1, const Expr *E2, ASTContext &Ctx);
367+
static bool areEqualIntegralBinaryOperators(const BinaryOperator *E1,
368+
const Expr *E2_LHS,
369+
BinaryOperatorKind BOP,
370+
const Expr *E2_RHS,
371+
ASTContext &Ctx) {
372+
if (E1->getOpcode() == BOP) {
373+
switch (BOP) {
374+
// Commutative operators:
375+
case BO_Mul:
376+
case BO_Add:
377+
return (areEqualIntegers(E1->getLHS(), E2_LHS, Ctx) &&
378+
areEqualIntegers(E1->getRHS(), E2_RHS, Ctx)) ||
379+
(areEqualIntegers(E1->getLHS(), E2_RHS, Ctx) &&
380+
areEqualIntegers(E1->getRHS(), E2_LHS, Ctx));
381+
default:
382+
return false;
383+
}
384+
}
385+
return false;
386+
}
387+
388+
static bool areEqualIntegers(const Expr *E1, const Expr *E2, ASTContext &Ctx) {
389+
E1 = E1->IgnoreParenImpCasts();
390+
E2 = E2->IgnoreParenImpCasts();
391+
if (!E1->getType()->isIntegerType() || E1->getType() != E2->getType())
392+
return false;
393+
394+
Expr::EvalResult ER1, ER2;
395+
396+
// If both are constants:
397+
if (E1->EvaluateAsInt(ER1, Ctx) &&
398+
E2->EvaluateAsInt(ER2, Ctx))
399+
return ER1.Val.getInt() == ER2.Val.getInt();
400+
401+
// Otherwise, they should have identical stmt kind:
402+
if (E1->getStmtClass() != E2->getStmtClass())
403+
return false;
404+
switch (E1->getStmtClass()) {
405+
case Stmt::DeclRefExprClass:
406+
return cast<DeclRefExpr>(E1)->getDecl() == cast<DeclRefExpr>(E2)->getDecl();
407+
case Stmt::BinaryOperatorClass: {
408+
auto BO2 = cast<BinaryOperator>(E2);
409+
return areEqualIntegralBinaryOperators(cast<BinaryOperator>(E1),
410+
BO2->getLHS(), BO2->getOpcode(),
411+
BO2->getRHS(), Ctx);
412+
}
413+
default:
414+
return false;
415+
}
416+
}
417+
356418
// Given a two-param std::span construct call, matches iff the call has the
357419
// following forms:
358420
// 1. `std::span<T>{new T[n], n}`, where `n` is a literal or a DRE
359421
// 2. `std::span<T>{new T, 1}`
360-
// 3. `std::span<T>{&var, 1}`
422+
// 3. `std::span<T>{&var, 1}` or `std::span<T>{std::addressof(...), 1}`
361423
// 4. `std::span<T>{a, n}`, where `a` is of an array-of-T with constant size
362424
// `n`
363425
// 5. `std::span<T>{any, 0}`
364-
// 6. `std::span<T>{std::addressof(...), 1}`
426+
// 6. `std::span<T>{ (char *)f(args), args[N] * arg*[M]}`, where
427+
// `f` is a function with attribute `alloc_size(N, M)`;
428+
// `args` represents the list of arguments;
429+
// `N, M` are parameter indexes to the allocating element number and size.
430+
// Sometimes, there is only one parameter index representing the total
431+
// size.
365432
AST_MATCHER(CXXConstructExpr, isSafeSpanTwoParamConstruct) {
366433
assert(Node.getNumArgs() == 2 &&
367434
"expecting a two-parameter std::span constructor");
368-
const Expr *Arg0 = Node.getArg(0)->IgnoreImplicit();
369-
const Expr *Arg1 = Node.getArg(1)->IgnoreImplicit();
370-
auto HaveEqualConstantValues = [&Finder](const Expr *E0, const Expr *E1) {
371-
if (auto E0CV = E0->getIntegerConstantExpr(Finder->getASTContext()))
372-
if (auto E1CV = E1->getIntegerConstantExpr(Finder->getASTContext())) {
435+
const Expr *Arg0 = Node.getArg(0)->IgnoreParenImpCasts();
436+
const Expr *Arg1 = Node.getArg(1)->IgnoreParenImpCasts();
437+
ASTContext &Ctx = Finder->getASTContext();
438+
439+
auto HaveEqualConstantValues = [&Ctx](const Expr *E0, const Expr *E1) {
440+
if (auto E0CV = E0->getIntegerConstantExpr(Ctx))
441+
if (auto E1CV = E1->getIntegerConstantExpr(Ctx)) {
373442
return APSInt::compareValues(*E0CV, *E1CV) == 0;
374443
}
375444
return false;
@@ -381,13 +450,14 @@ AST_MATCHER(CXXConstructExpr, isSafeSpanTwoParamConstruct) {
381450
}
382451
return false;
383452
};
384-
std::optional<APSInt> Arg1CV =
385-
Arg1->getIntegerConstantExpr(Finder->getASTContext());
453+
std::optional<APSInt> Arg1CV = Arg1->getIntegerConstantExpr(Ctx);
386454

387455
if (Arg1CV && Arg1CV->isZero())
388456
// Check form 5:
389457
return true;
390-
switch (Arg0->IgnoreImplicit()->getStmtClass()) {
458+
459+
// Check forms 1-3:
460+
switch (Arg0->getStmtClass()) {
391461
case Stmt::CXXNewExprClass:
392462
if (auto Size = cast<CXXNewExpr>(Arg0)->getArraySize()) {
393463
// Check form 1:
@@ -407,6 +477,7 @@ AST_MATCHER(CXXConstructExpr, isSafeSpanTwoParamConstruct) {
407477
return Arg1CV && Arg1CV->isOne();
408478
break;
409479
case Stmt::CallExprClass:
480+
// Check form 3:
410481
if (const auto *CE = dyn_cast<CallExpr>(Arg0)) {
411482
const auto FnDecl = CE->getDirectCallee();
412483
if (FnDecl && FnDecl->getNameAsString() == "addressof" &&
@@ -421,13 +492,41 @@ AST_MATCHER(CXXConstructExpr, isSafeSpanTwoParamConstruct) {
421492

422493
QualType Arg0Ty = Arg0->IgnoreImplicit()->getType();
423494

424-
if (auto *ConstArrTy =
425-
Finder->getASTContext().getAsConstantArrayType(Arg0Ty)) {
495+
if (auto *ConstArrTy = Ctx.getAsConstantArrayType(Arg0Ty)) {
426496
const APSInt ConstArrSize = APSInt(ConstArrTy->getSize());
427497

428498
// Check form 4:
429499
return Arg1CV && APSInt::compareValues(ConstArrSize, *Arg1CV) == 0;
430500
}
501+
// Check form 6:
502+
if (auto CCast = dyn_cast<CStyleCastExpr>(Arg0)) {
503+
if (!CCast->getType()->isPointerType())
504+
return false;
505+
506+
QualType PteTy = CCast->getType()->getPointeeType();
507+
508+
if (!(PteTy->isConstantSizeType() && Ctx.getTypeSizeInChars(PteTy).isOne()))
509+
return false;
510+
511+
if (const auto *Call = dyn_cast<CallExpr>(CCast->getSubExpr())) {
512+
if (const FunctionDecl *FD = Call->getDirectCallee())
513+
if (auto *AllocAttr = FD->getAttr<AllocSizeAttr>()) {
514+
const Expr *EleSizeExpr =
515+
Call->getArg(AllocAttr->getElemSizeParam().getASTIndex());
516+
// NumElemIdx is invalid if AllocSizeAttr has 1 argument:
517+
ParamIdx NumElemIdx = AllocAttr->getNumElemsParam();
518+
519+
if (!NumElemIdx.isValid())
520+
return areEqualIntegers(Arg1, EleSizeExpr, Ctx);
521+
522+
const Expr *NumElesExpr = Call->getArg(NumElemIdx.getASTIndex());
523+
524+
if (auto BO = dyn_cast<BinaryOperator>(Arg1))
525+
return areEqualIntegralBinaryOperators(BO, NumElesExpr, BO_Mul,
526+
EleSizeExpr, Ctx);
527+
}
528+
}
529+
}
431530
return false;
432531
}
433532

clang/lib/Driver/Action.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ const char *Action::getClassName(ActionClass AC) {
5252
return "static-lib-linker";
5353
case BinaryAnalyzeJobClass:
5454
return "binary-analyzer";
55+
case BinaryTranslatorJobClass:
56+
return "binary-translator";
5557
}
5658

5759
llvm_unreachable("invalid class");
@@ -484,3 +486,9 @@ void BinaryAnalyzeJobAction::anchor() {}
484486

485487
BinaryAnalyzeJobAction::BinaryAnalyzeJobAction(Action *Input, types::ID Type)
486488
: JobAction(BinaryAnalyzeJobClass, Input, Type) {}
489+
490+
void BinaryTranslatorJobAction::anchor() {}
491+
492+
BinaryTranslatorJobAction::BinaryTranslatorJobAction(Action *Input,
493+
types::ID Type)
494+
: JobAction(BinaryTranslatorJobClass, Input, Type) {}

clang/lib/Driver/Driver.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4695,6 +4695,16 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
46954695
Actions.push_back(C.MakeAction<BinaryAnalyzeJobAction>(
46964696
LastAction, types::TY_DX_CONTAINER));
46974697
}
4698+
if (Args.getLastArg(options::OPT_metal)) {
4699+
Action *LastAction = Actions.back();
4700+
// Metal shader converter runs on DXIL containers, which can either be
4701+
// validated (in which case they are TY_DX_CONTAINER), or unvalidated
4702+
// (TY_OBJECT).
4703+
if (LastAction->getType() == types::TY_DX_CONTAINER ||
4704+
LastAction->getType() == types::TY_Object)
4705+
Actions.push_back(C.MakeAction<BinaryTranslatorJobAction>(
4706+
LastAction, types::TY_DX_CONTAINER));
4707+
}
46984708
}
46994709

47004710
// Claim ignored clang-cl options.

clang/lib/Driver/ToolChain.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ Tool *ToolChain::getTool(Action::ActionClass AC) const {
636636
case Action::DsymutilJobClass:
637637
case Action::VerifyDebugInfoJobClass:
638638
case Action::BinaryAnalyzeJobClass:
639+
case Action::BinaryTranslatorJobClass:
639640
llvm_unreachable("Invalid tool kind.");
640641

641642
case Action::FortranFrontendJobClass:

clang/lib/Driver/ToolChains/HLSL.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,22 @@ void tools::hlsl::Validator::ConstructJob(Compilation &C, const JobAction &JA,
198198
Exec, CmdArgs, Inputs, Input));
199199
}
200200

201+
void tools::hlsl::MetalConverter::ConstructJob(
202+
Compilation &C, const JobAction &JA, const InputInfo &Output,
203+
const InputInfoList &Inputs, const ArgList &Args,
204+
const char *LinkingOutput) const {
205+
std::string MSCPath = getToolChain().GetProgramPath("metal-shaderconverter");
206+
ArgStringList CmdArgs;
207+
const InputInfo &Input = Inputs[0];
208+
CmdArgs.push_back(Input.getFilename());
209+
CmdArgs.push_back("-o");
210+
CmdArgs.push_back(Input.getFilename());
211+
212+
const char *Exec = Args.MakeArgString(MSCPath);
213+
C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
214+
Exec, CmdArgs, Inputs, Input));
215+
}
216+
201217
/// DirectX Toolchain
202218
HLSLToolChain::HLSLToolChain(const Driver &D, const llvm::Triple &Triple,
203219
const ArgList &Args)
@@ -214,6 +230,10 @@ Tool *clang::driver::toolchains::HLSLToolChain::getTool(
214230
if (!Validator)
215231
Validator.reset(new tools::hlsl::Validator(*this));
216232
return Validator.get();
233+
case Action::BinaryTranslatorJobClass:
234+
if (!MetalConverter)
235+
MetalConverter.reset(new tools::hlsl::MetalConverter(*this));
236+
return MetalConverter.get();
217237
default:
218238
return ToolChain::getTool(AC);
219239
}

clang/lib/Driver/ToolChains/HLSL.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ class LLVM_LIBRARY_VISIBILITY Validator : public Tool {
2929
const llvm::opt::ArgList &TCArgs,
3030
const char *LinkingOutput) const override;
3131
};
32+
33+
class LLVM_LIBRARY_VISIBILITY MetalConverter : public Tool {
34+
public:
35+
MetalConverter(const ToolChain &TC)
36+
: Tool("hlsl::MetalConverter", "metal-shaderconverter", TC) {}
37+
38+
bool hasIntegratedCPP() const override { return false; }
39+
40+
void ConstructJob(Compilation &C, const JobAction &JA,
41+
const InputInfo &Output, const InputInfoList &Inputs,
42+
const llvm::opt::ArgList &TCArgs,
43+
const char *LinkingOutput) const override;
44+
};
3245
} // namespace hlsl
3346
} // namespace tools
3447

@@ -57,6 +70,7 @@ class LLVM_LIBRARY_VISIBILITY HLSLToolChain : public ToolChain {
5770

5871
private:
5972
mutable std::unique_ptr<tools::hlsl::Validator> Validator;
73+
mutable std::unique_ptr<tools::hlsl::MetalConverter> MetalConverter;
6074
};
6175

6276
} // end namespace toolchains

clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ bool tryToFindPtrOrigin(
4343
break;
4444
}
4545
}
46+
if (auto *TempExpr = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
47+
if (isSafePtrType(TempExpr->getTypeAsWritten()))
48+
return callback(TempExpr, true);
49+
}
4650
if (auto *POE = dyn_cast<PseudoObjectExpr>(E)) {
4751
if (auto *RF = POE->getResultExpr()) {
4852
E = RF;

clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,14 @@ static bool isPtrOfType(const clang::QualType T, Predicate Pred) {
162162
type = elaboratedT->desugar();
163163
continue;
164164
}
165-
auto *SpecialT = type->getAs<TemplateSpecializationType>();
166-
if (!SpecialT)
167-
return false;
168-
auto *Decl = SpecialT->getTemplateName().getAsTemplateDecl();
169-
if (!Decl)
170-
return false;
171-
return Pred(Decl->getNameAsString());
165+
if (auto *SpecialT = type->getAs<TemplateSpecializationType>()) {
166+
auto *Decl = SpecialT->getTemplateName().getAsTemplateDecl();
167+
return Decl && Pred(Decl->getNameAsString());
168+
} else if (auto *DTS = type->getAs<DeducedTemplateSpecializationType>()) {
169+
auto *Decl = DTS->getTemplateName().getAsTemplateDecl();
170+
return Decl && Pred(Decl->getNameAsString());
171+
} else
172+
break;
172173
}
173174
return false;
174175
}

0 commit comments

Comments
 (0)