Skip to content

Commit 32c812f

Browse files
authored
merge main into amd-staging (#666)
2 parents 1c2677f + c08f05e commit 32c812f

File tree

85 files changed

+3400
-1000
lines changed

Some content is hidden

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

85 files changed

+3400
-1000
lines changed

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,20 @@ class SemaHLSL : public SemaBase {
250250
const RecordType *RT);
251251

252252
void checkSemanticAnnotation(FunctionDecl *EntryPoint, const Decl *Param,
253-
const HLSLAppliedSemanticAttr *SemanticAttr);
253+
const HLSLAppliedSemanticAttr *SemanticAttr,
254+
bool IsInput);
255+
254256
bool determineActiveSemanticOnScalar(FunctionDecl *FD,
255257
DeclaratorDecl *OutputDecl,
256258
DeclaratorDecl *D,
257259
SemanticInfo &ActiveSemantic,
258-
llvm::StringSet<> &ActiveInputSemantics);
260+
llvm::StringSet<> &ActiveSemantics,
261+
bool IsInput);
262+
259263
bool determineActiveSemantic(FunctionDecl *FD, DeclaratorDecl *OutputDecl,
260264
DeclaratorDecl *D, SemanticInfo &ActiveSemantic,
261-
llvm::StringSet<> &ActiveInputSemantics);
265+
llvm::StringSet<> &ActiveSemantics,
266+
bool IsInput);
262267

263268
void processExplicitBindingsOnDecl(VarDecl *D);
264269

clang/lib/CIR/CodeGen/CIRGenDeclOpenACC.cpp

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,64 @@ using namespace clang::CIRGen;
1919

2020
namespace {
2121
struct OpenACCDeclareCleanup final : EHScopeStack::Cleanup {
22+
SourceRange declareRange;
2223
mlir::acc::DeclareEnterOp enterOp;
2324

24-
OpenACCDeclareCleanup(mlir::acc::DeclareEnterOp enterOp) : enterOp(enterOp) {}
25+
OpenACCDeclareCleanup(SourceRange declareRange,
26+
mlir::acc::DeclareEnterOp enterOp)
27+
: declareRange(declareRange), enterOp(enterOp) {}
28+
29+
template <typename OutTy, typename InTy>
30+
void createOutOp(CIRGenFunction &cgf, InTy inOp) {
31+
if constexpr (std::is_same_v<OutTy, mlir::acc::DeleteOp>) {
32+
auto outOp =
33+
OutTy::create(cgf.getBuilder(), inOp.getLoc(), inOp,
34+
inOp.getStructured(), inOp.getImplicit(),
35+
llvm::Twine(inOp.getNameAttr()), inOp.getBounds());
36+
outOp.setDataClause(inOp.getDataClause());
37+
outOp.setModifiers(inOp.getModifiers());
38+
} else {
39+
auto outOp =
40+
OutTy::create(cgf.getBuilder(), inOp.getLoc(), inOp, inOp.getVarPtr(),
41+
inOp.getStructured(), inOp.getImplicit(),
42+
llvm::Twine(inOp.getNameAttr()), inOp.getBounds());
43+
outOp.setDataClause(inOp.getDataClause());
44+
outOp.setModifiers(inOp.getModifiers());
45+
}
46+
}
2547

2648
void emit(CIRGenFunction &cgf) override {
27-
mlir::acc::DeclareExitOp::create(cgf.getBuilder(), enterOp.getLoc(),
28-
enterOp, {});
49+
auto exitOp = mlir::acc::DeclareExitOp::create(
50+
cgf.getBuilder(), enterOp.getLoc(), enterOp, {});
2951

30-
// TODO(OpenACC): Some clauses require that we add info about them to the
31-
// DeclareExitOp. However, we don't have any of those implemented yet, so
32-
// we should add infrastructure here to do that once we have one
33-
// implemented.
52+
// Some data clauses need to be referenced in 'exit', AND need to have an
53+
// operation after the exit. Copy these from the enter operation.
54+
for (mlir::Value val : enterOp.getDataClauseOperands()) {
55+
if (auto copyin = val.getDefiningOp<mlir::acc::CopyinOp>()) {
56+
switch (copyin.getDataClause()) {
57+
default:
58+
cgf.cgm.errorNYI(declareRange,
59+
"OpenACC local declare clause copyin cleanup");
60+
break;
61+
case mlir::acc::DataClause::acc_copy:
62+
createOutOp<mlir::acc::CopyoutOp>(cgf, copyin);
63+
break;
64+
case mlir::acc::DataClause::acc_copyin:
65+
createOutOp<mlir::acc::DeleteOp>(cgf, copyin);
66+
break;
67+
}
68+
} else if (val.getDefiningOp<mlir::acc::DeclareLinkOp>()) {
69+
// Link has no exit clauses, and shouldn't be copied.
70+
continue;
71+
} else if (val.getDefiningOp<mlir::acc::DevicePtrOp>()) {
72+
// DevicePtr has no exit clauses, and shouldn't be copied.
73+
continue;
74+
} else {
75+
cgf.cgm.errorNYI(declareRange, "OpenACC local declare clause cleanup");
76+
continue;
77+
}
78+
exitOp.getDataClauseOperandsMutable().append(val);
79+
}
3480
}
3581
};
3682
} // namespace
@@ -45,7 +91,7 @@ void CIRGenFunction::emitOpenACCDeclare(const OpenACCDeclareDecl &d) {
4591
d.clauses());
4692

4793
ehStack.pushCleanup<OpenACCDeclareCleanup>(CleanupKind::NormalCleanup,
48-
enterOp);
94+
d.getSourceRange(), enterOp);
4995
}
5096

5197
void CIRGenFunction::emitOpenACCRoutine(const OpenACCRoutineDecl &d) {

clang/lib/CIR/CodeGen/CIRGenOpenACCClause.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -800,12 +800,16 @@ class OpenACCClauseCIREmitter final
800800
var, mlir::acc::DataClause::acc_copy, clause.getModifierList(),
801801
/*structured=*/true,
802802
/*implicit=*/false);
803+
} else if constexpr (isOneOfTypes<OpTy, mlir::acc::DeclareEnterOp>) {
804+
for (const Expr *var : clause.getVarList())
805+
addDataOperand<mlir::acc::CopyinOp>(
806+
var, mlir::acc::DataClause::acc_copy, clause.getModifierList(),
807+
/*structured=*/true,
808+
/*implicit=*/false);
803809
} else if constexpr (isCombinedType<OpTy>) {
804810
applyToComputeOp(clause);
805811
} else {
806-
// TODO: When we've implemented this for everything, switch this to an
807-
// unreachable. declare construct remains.
808-
return clauseNotImplemented(clause);
812+
llvm_unreachable("Unknown construct kind in VisitCopyClause");
809813
}
810814
}
811815

@@ -822,12 +826,16 @@ class OpenACCClauseCIREmitter final
822826
addDataOperand<mlir::acc::CopyinOp>(
823827
var, mlir::acc::DataClause::acc_copyin, clause.getModifierList(),
824828
/*structured=*/false, /*implicit=*/false);
829+
} else if constexpr (isOneOfTypes<OpTy, mlir::acc::DeclareEnterOp>) {
830+
for (const Expr *var : clause.getVarList())
831+
addDataOperand<mlir::acc::CopyinOp>(
832+
var, mlir::acc::DataClause::acc_copyin, clause.getModifierList(),
833+
/*structured=*/true,
834+
/*implicit=*/false);
825835
} else if constexpr (isCombinedType<OpTy>) {
826836
applyToComputeOp(clause);
827837
} else {
828-
// TODO: When we've implemented this for everything, switch this to an
829-
// unreachable. declare construct remains.
830-
return clauseNotImplemented(clause);
838+
llvm_unreachable("Unknown construct kind in VisitCopyInClause");
831839
}
832840
}
833841

clang/lib/CodeGen/CGException.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E,
450450
// Therefore, we emit a trap which will abort the program, and
451451
// prompt a warning indicating that a trap will be emitted.
452452
const llvm::Triple &T = Target.getTriple();
453-
if (CGM.getLangOpts().OpenMPIsTargetDevice && (T.isNVPTX() || T.isAMDGCN())) {
453+
if (CGM.getLangOpts().OpenMPIsTargetDevice && T.isGPU()) {
454454
EmitTrapCall(llvm::Intrinsic::trap);
455455
return;
456456
}
@@ -627,7 +627,7 @@ void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
627627
// If we encounter a try statement on in an OpenMP target region offloaded to
628628
// a GPU, we treat it as a basic block.
629629
const bool IsTargetDevice =
630-
(CGM.getLangOpts().OpenMPIsTargetDevice && (T.isNVPTX() || T.isAMDGCN()));
630+
(CGM.getLangOpts().OpenMPIsTargetDevice && T.isGPU());
631631
if (!IsTargetDevice)
632632
EnterCXXTryStmt(S);
633633
EmitStmt(S.getTryBlock());

clang/lib/CodeGen/CGExprComplex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ class ComplexExprEmitter
320320
QualType getPromotionType(FPOptionsOverride Features, QualType Ty,
321321
bool IsComplexDivisor) {
322322
if (auto *CT = Ty->getAs<ComplexType>()) {
323-
QualType ElementType = CT->getElementType();
323+
QualType ElementType = CT->getElementType().getCanonicalType();
324324
bool IsFloatingType = ElementType->isFloatingType();
325325
bool IsComplexRangePromoted = CGF.getLangOpts().getComplexRange() ==
326326
LangOptions::ComplexRangeKind::CX_Promoted;

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -731,13 +731,22 @@ llvm::Value *CGHLSLRuntime::emitSystemSemanticLoad(
731731
}
732732

733733
if (SemanticName == "SV_POSITION") {
734-
if (CGM.getTriple().getEnvironment() == Triple::EnvironmentType::Pixel)
735-
return createSPIRVBuiltinLoad(B, CGM.getModule(), Type,
736-
Semantic->getAttrName()->getName(),
737-
/* BuiltIn::FragCoord */ 15);
734+
if (CGM.getTriple().getEnvironment() == Triple::EnvironmentType::Pixel) {
735+
if (CGM.getTarget().getTriple().isSPIRV())
736+
return createSPIRVBuiltinLoad(B, CGM.getModule(), Type,
737+
Semantic->getAttrName()->getName(),
738+
/* BuiltIn::FragCoord */ 15);
739+
if (CGM.getTarget().getTriple().isDXIL())
740+
return emitDXILUserSemanticLoad(B, Type, Semantic, Index);
741+
}
742+
743+
if (CGM.getTriple().getEnvironment() == Triple::EnvironmentType::Vertex) {
744+
return emitUserSemanticLoad(B, Type, Decl, Semantic, Index);
745+
}
738746
}
739747

740-
llvm_unreachable("non-handled system semantic. FIXME.");
748+
llvm_unreachable(
749+
"Load hasn't been implemented yet for this system semantic. FIXME");
741750
}
742751

743752
static void createSPIRVBuiltinStore(IRBuilder<> &B, llvm::Module &M,
@@ -760,12 +769,22 @@ void CGHLSLRuntime::emitSystemSemanticStore(IRBuilder<> &B, llvm::Value *Source,
760769
std::optional<unsigned> Index) {
761770

762771
std::string SemanticName = Semantic->getAttrName()->getName().upper();
763-
if (SemanticName == "SV_POSITION")
764-
createSPIRVBuiltinStore(B, CGM.getModule(), Source,
765-
Semantic->getAttrName()->getName(),
766-
/* BuiltIn::Position */ 0);
767-
else
768-
llvm_unreachable("non-handled system semantic. FIXME.");
772+
if (SemanticName == "SV_POSITION") {
773+
if (CGM.getTarget().getTriple().isDXIL()) {
774+
emitDXILUserSemanticStore(B, Source, Semantic, Index);
775+
return;
776+
}
777+
778+
if (CGM.getTarget().getTriple().isSPIRV()) {
779+
createSPIRVBuiltinStore(B, CGM.getModule(), Source,
780+
Semantic->getAttrName()->getName(),
781+
/* BuiltIn::Position */ 0);
782+
return;
783+
}
784+
}
785+
786+
llvm_unreachable(
787+
"Store hasn't been implemented yet for this system semantic. FIXME");
769788
}
770789

771790
llvm::Value *CGHLSLRuntime::handleScalarSemanticLoad(

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4360,8 +4360,7 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
43604360

43614361
// Set the flag to prevent the implementation from emitting device exception
43624362
// handling code for those requiring so.
4363-
if ((Opts.OpenMPIsTargetDevice && (T.isNVPTX() || T.isAMDGCN())) ||
4364-
Opts.OpenCLCPlusPlus) {
4363+
if ((Opts.OpenMPIsTargetDevice && T.isGPU()) || Opts.OpenCLCPlusPlus) {
43654364

43664365
Opts.Exceptions = 0;
43674366
Opts.CXXExceptions = 0;

clang/lib/Sema/SemaExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10743,7 +10743,7 @@ static void DetectPrecisionLossInComplexDivision(Sema &S, QualType DivisorTy,
1074310743
if (!CT)
1074410744
return;
1074510745

10746-
QualType ElementType = CT->getElementType();
10746+
QualType ElementType = CT->getElementType().getCanonicalType();
1074710747
bool IsComplexRangePromoted = S.getLangOpts().getComplexRange() ==
1074810748
LangOptions::ComplexRangeKind::CX_Promoted;
1074910749
if (!ElementType->isFloatingType() || !IsComplexRangePromoted)

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -771,9 +771,12 @@ void SemaHLSL::ActOnTopLevelFunction(FunctionDecl *FD) {
771771
}
772772
}
773773

774-
bool SemaHLSL::determineActiveSemanticOnScalar(
775-
FunctionDecl *FD, DeclaratorDecl *OutputDecl, DeclaratorDecl *D,
776-
SemanticInfo &ActiveSemantic, llvm::StringSet<> &UsedSemantics) {
774+
bool SemaHLSL::determineActiveSemanticOnScalar(FunctionDecl *FD,
775+
DeclaratorDecl *OutputDecl,
776+
DeclaratorDecl *D,
777+
SemanticInfo &ActiveSemantic,
778+
llvm::StringSet<> &UsedSemantics,
779+
bool IsInput) {
777780
if (ActiveSemantic.Semantic == nullptr) {
778781
ActiveSemantic.Semantic = D->getAttr<HLSLParsedSemanticAttr>();
779782
if (ActiveSemantic.Semantic)
@@ -792,7 +795,7 @@ bool SemaHLSL::determineActiveSemanticOnScalar(
792795
if (!A)
793796
return false;
794797

795-
checkSemanticAnnotation(FD, D, A);
798+
checkSemanticAnnotation(FD, D, A, IsInput);
796799
OutputDecl->addAttr(A);
797800

798801
unsigned Location = ActiveSemantic.Index.value_or(0);
@@ -820,7 +823,8 @@ bool SemaHLSL::determineActiveSemantic(FunctionDecl *FD,
820823
DeclaratorDecl *OutputDecl,
821824
DeclaratorDecl *D,
822825
SemanticInfo &ActiveSemantic,
823-
llvm::StringSet<> &UsedSemantics) {
826+
llvm::StringSet<> &UsedSemantics,
827+
bool IsInput) {
824828
if (ActiveSemantic.Semantic == nullptr) {
825829
ActiveSemantic.Semantic = D->getAttr<HLSLParsedSemanticAttr>();
826830
if (ActiveSemantic.Semantic)
@@ -833,12 +837,13 @@ bool SemaHLSL::determineActiveSemantic(FunctionDecl *FD,
833837
const RecordType *RT = dyn_cast<RecordType>(T);
834838
if (!RT)
835839
return determineActiveSemanticOnScalar(FD, OutputDecl, D, ActiveSemantic,
836-
UsedSemantics);
840+
UsedSemantics, IsInput);
837841

838842
const RecordDecl *RD = RT->getDecl();
839843
for (FieldDecl *Field : RD->fields()) {
840844
SemanticInfo Info = ActiveSemantic;
841-
if (!determineActiveSemantic(FD, OutputDecl, Field, Info, UsedSemantics)) {
845+
if (!determineActiveSemantic(FD, OutputDecl, Field, Info, UsedSemantics,
846+
IsInput)) {
842847
Diag(Field->getLocation(), diag::note_hlsl_semantic_used_here) << Field;
843848
return false;
844849
}
@@ -920,7 +925,7 @@ void SemaHLSL::CheckEntryPoint(FunctionDecl *FD) {
920925

921926
// FIXME: Verify output semantics in parameters.
922927
if (!determineActiveSemantic(FD, Param, Param, ActiveSemantic,
923-
ActiveInputSemantics)) {
928+
ActiveInputSemantics, /* IsInput= */ true)) {
924929
Diag(Param->getLocation(), diag::note_previous_decl) << Param;
925930
FD->setInvalidDecl();
926931
}
@@ -932,12 +937,13 @@ void SemaHLSL::CheckEntryPoint(FunctionDecl *FD) {
932937
if (ActiveSemantic.Semantic)
933938
ActiveSemantic.Index = ActiveSemantic.Semantic->getSemanticIndex();
934939
if (!FD->getReturnType()->isVoidType())
935-
determineActiveSemantic(FD, FD, FD, ActiveSemantic, ActiveOutputSemantics);
940+
determineActiveSemantic(FD, FD, FD, ActiveSemantic, ActiveOutputSemantics,
941+
/* IsInput= */ false);
936942
}
937943

938944
void SemaHLSL::checkSemanticAnnotation(
939945
FunctionDecl *EntryPoint, const Decl *Param,
940-
const HLSLAppliedSemanticAttr *SemanticAttr) {
946+
const HLSLAppliedSemanticAttr *SemanticAttr, bool IsInput) {
941947
auto *ShaderAttr = EntryPoint->getAttr<HLSLShaderAttr>();
942948
assert(ShaderAttr && "Entry point has no shader attribute");
943949
llvm::Triple::EnvironmentType ST = ShaderAttr->getType();
@@ -961,11 +967,12 @@ void SemaHLSL::checkSemanticAnnotation(
961967
}
962968

963969
if (SemanticName == "SV_POSITION") {
964-
// TODO(#143523): allow use on other shader types & output once the overall
965-
// semantic logic is implemented.
966-
if (ST == llvm::Triple::Pixel)
970+
// SV_Position can be an input or output in vertex shaders,
971+
// but only an input in pixel shaders.
972+
if (ST == llvm::Triple::Vertex || (ST == llvm::Triple::Pixel && IsInput))
967973
return;
968-
DiagnoseAttrStageMismatch(SemanticAttr, ST, {llvm::Triple::Pixel});
974+
DiagnoseAttrStageMismatch(SemanticAttr, ST,
975+
{llvm::Triple::Pixel, llvm::Triple::Vertex});
969976
return;
970977
}
971978

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %clang_cc1 -triple spirv-unknown-vulkan1.3-vertex -finclude-default-header -ast-dump -o - %s | FileCheck %s
2+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.8-vertex -finclude-default-header -ast-dump -o - %s | FileCheck %s
3+
4+
5+
// CHECK: CXXRecordDecl {{.*}} referenced struct S definition
6+
// CHECK: FieldDecl {{.*}} field1 'int'
7+
// CHECK-NEXT: HLSLParsedSemanticAttr {{.*}} "A" 0
8+
// CHECK: FieldDecl {{.*}} field2 'int'
9+
// CHECK-NEXT: HLSLParsedSemanticAttr {{.*}} "B" 4
10+
11+
struct S {
12+
int field1 : A;
13+
int field2 : B4;
14+
};
15+
16+
// CHECK: FunctionDecl {{.*}} main 'void (S)'
17+
// CHECK-NEXT: ParmVarDecl {{.*}} s 'S'
18+
// CHECK-NEXT: HLSLParsedSemanticAttr {{.*}} "C" 0
19+
// CHECK-NEXT: HLSLAppliedSemanticAttr {{.*}} "C" 0
20+
// CHECK-NEXT: HLSLAppliedSemanticAttr {{.*}} "C" 1
21+
void main(S s : C) {}

0 commit comments

Comments
 (0)