Skip to content

Commit 832dd1e

Browse files
authored
merge main into amd-staging (#359)
2 parents d8ece93 + 8048496 commit 832dd1e

File tree

173 files changed

+2960
-2278
lines changed

Some content is hidden

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

173 files changed

+2960
-2278
lines changed

clang/include/clang/AST/Attr.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ class HLSLSemanticAttr : public HLSLAnnotationAttr {
239239
LLVM_PREFERRED_TYPE(bool)
240240
unsigned SemanticExplicitIndex : 1;
241241

242+
Decl *TargetDecl = nullptr;
243+
242244
protected:
243245
HLSLSemanticAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
244246
attr::Kind AK, bool IsLateParsed,
@@ -259,6 +261,11 @@ class HLSLSemanticAttr : public HLSLAnnotationAttr {
259261

260262
unsigned getSemanticIndex() const { return SemanticIndex; }
261263

264+
bool isSemanticIndexExplicit() const { return SemanticExplicitIndex; }
265+
266+
void setTargetDecl(Decl *D) { TargetDecl = D; }
267+
Decl *getTargetDecl() const { return TargetDecl; }
268+
262269
// Implement isa/cast/dyncast/etc.
263270
static bool classof(const Attr *A) {
264271
return A->getKind() >= attr::FirstHLSLSemanticAttr &&

clang/include/clang/Basic/Attr.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,8 @@ class HLSLSemanticAttr<bit Indexable> : HLSLAnnotationAttr {
787787
let Spellings = [];
788788
let Subjects = SubjectList<[ParmVar, Field, Function]>;
789789
let LangOpts = [HLSL];
790+
let Args = [DeclArgument<Named, "Target">, IntArgument<"SemanticIndex">,
791+
BoolArgument<"SemanticExplicitIndex">];
790792
}
791793

792794
/// A target-specific attribute. This class is meant to be used as a mixin

clang/include/clang/Basic/DiagnosticFrontendKinds.td

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,6 @@ def warn_hlsl_langstd_minimal :
404404
"recommend using %1 instead">,
405405
InGroup<HLSLDXCCompat>;
406406

407-
def err_hlsl_semantic_missing : Error<"semantic annotations must be present "
408-
"for all input and outputs of an entry "
409-
"function or patch constant function">;
410-
411407
// ClangIR frontend errors
412408
def err_cir_to_cir_transform_failed : Error<
413409
"CIR-to-CIR transformation failed">, DefaultFatal;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13173,6 +13173,7 @@ def err_hlsl_duplicate_parameter_modifier : Error<"duplicate parameter modifier
1317313173
def err_hlsl_missing_semantic_annotation : Error<
1317413174
"semantic annotations must be present for all parameters of an entry "
1317513175
"function or patch constant function">;
13176+
def note_hlsl_semantic_used_here : Note<"%0 used here">;
1317613177
def err_hlsl_unknown_semantic : Error<"unknown HLSL semantic %0">;
1317713178
def err_hlsl_semantic_output_not_supported
1317813179
: Error<"semantic %0 does not support output">;

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4052,6 +4052,43 @@ def CIR_ExpectOp : CIR_Op<"expect", [
40524052
}];
40534053
}
40544054

4055+
//===----------------------------------------------------------------------===//
4056+
// PrefetchOp
4057+
//===----------------------------------------------------------------------===//
4058+
4059+
def CIR_PrefetchOp : CIR_Op<"prefetch"> {
4060+
let summary = "Prefetch operation";
4061+
let description = [{
4062+
The `cir.prefetch` operation is a hint to the code generator to insert a
4063+
prefetch instruction if supported; otherwise, it is a noop. Prefetches
4064+
have no effect on the behavior of the program but can change its
4065+
performance characteristics.
4066+
4067+
```mlir
4068+
cir.prefetch(%0 : !cir.ptr<!void>) locality(1) write
4069+
```
4070+
4071+
$locality is a temporal locality specifier ranging from (0) - no locality,
4072+
to (3) - extremely local, keep in cache. If $locality is not present, the
4073+
default value is 3.
4074+
4075+
$isWrite specifies whether the prefetch is for a 'read' or 'write'. If
4076+
$isWrite is not specified, it means that prefetch is prepared for 'read'.
4077+
}];
4078+
4079+
let arguments = (ins CIR_VoidPtrType:$addr,
4080+
DefaultValuedAttr<ConfinedAttr<I32Attr, [IntMinValue<0>, IntMaxValue<3>]>,
4081+
"3">:$locality,
4082+
UnitAttr:$isWrite);
4083+
4084+
let assemblyFormat = [{
4085+
(`write` $isWrite^) : (`read`)?
4086+
`locality` `(` $locality `)`
4087+
$addr `:` qualified(type($addr))
4088+
attr-dict
4089+
}];
4090+
}
4091+
40554092
//===----------------------------------------------------------------------===//
40564093
// PtrDiffOp
40574094
//===----------------------------------------------------------------------===//

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,6 @@ class SemaHLSL : public SemaBase {
130130
bool ActOnUninitializedVarDecl(VarDecl *D);
131131
void ActOnEndOfTranslationUnit(TranslationUnitDecl *TU);
132132
void CheckEntryPoint(FunctionDecl *FD);
133-
bool isSemanticValid(FunctionDecl *FD, DeclaratorDecl *D);
134-
void CheckSemanticAnnotation(FunctionDecl *EntryPoint, const Decl *Param,
135-
const HLSLAnnotationAttr *AnnotationAttr);
136133
bool CheckResourceBinOp(BinaryOperatorKind Opc, Expr *LHSExpr, Expr *RHSExpr,
137134
SourceLocation Loc);
138135
void DiagnoseAttrStageMismatch(
@@ -179,17 +176,17 @@ class SemaHLSL : public SemaBase {
179176
bool handleResourceTypeAttr(QualType T, const ParsedAttr &AL);
180177

181178
template <typename T>
182-
T *createSemanticAttr(const ParsedAttr &AL,
179+
T *createSemanticAttr(const AttributeCommonInfo &ACI, NamedDecl *TargetDecl,
183180
std::optional<unsigned> Location) {
184-
T *Attr = ::new (getASTContext()) T(getASTContext(), AL);
185-
if (Attr->isSemanticIndexable())
186-
Attr->setSemanticIndex(Location ? *Location : 0);
187-
else if (Location.has_value()) {
181+
T *Attr =
182+
::new (getASTContext()) T(getASTContext(), ACI, TargetDecl,
183+
Location.value_or(0), Location.has_value());
184+
185+
if (!Attr->isSemanticIndexable() && Location.has_value()) {
188186
Diag(Attr->getLocation(), diag::err_hlsl_semantic_indexing_not_supported)
189187
<< Attr->getAttrName()->getName();
190188
return nullptr;
191189
}
192-
193190
return Attr;
194191
}
195192

@@ -247,10 +244,25 @@ class SemaHLSL : public SemaBase {
247244

248245
IdentifierInfo *RootSigOverrideIdent = nullptr;
249246

247+
struct SemanticInfo {
248+
HLSLSemanticAttr *Semantic;
249+
std::optional<uint32_t> Index;
250+
};
251+
250252
private:
251253
void collectResourceBindingsOnVarDecl(VarDecl *D);
252254
void collectResourceBindingsOnUserRecordDecl(const VarDecl *VD,
253255
const RecordType *RT);
256+
257+
void checkSemanticAnnotation(FunctionDecl *EntryPoint, const Decl *Param,
258+
const HLSLSemanticAttr *SemanticAttr);
259+
HLSLSemanticAttr *createSemantic(const SemanticInfo &Semantic,
260+
DeclaratorDecl *TargetDecl);
261+
bool determineActiveSemanticOnScalar(FunctionDecl *FD, DeclaratorDecl *D,
262+
SemanticInfo &ActiveSemantic);
263+
bool determineActiveSemantic(FunctionDecl *FD, DeclaratorDecl *D,
264+
SemanticInfo &ActiveSemantic);
265+
254266
void processExplicitBindingsOnDecl(VarDecl *D);
255267

256268
void diagnoseAvailabilityViolations(TranslationUnitDecl *TU);

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,7 +1842,6 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
18421842
const Expr *Init, PrimType T,
18431843
bool Activate = false) -> bool {
18441844
InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(Init));
1845-
InitLinkScope<Emitter> ILS(this, InitLink::Field(FieldToInit->Offset));
18461845
if (!this->visit(Init))
18471846
return false;
18481847

@@ -5385,55 +5384,57 @@ bool Compiler<Emitter>::VisitCXXThisExpr(const CXXThisExpr *E) {
53855384
// instance pointer of the current function frame, but e.g. to the declaration
53865385
// currently being initialized. Here we emit the necessary instruction(s) for
53875386
// this scenario.
5388-
if (!InitStackActive)
5387+
if (!InitStackActive || InitStack.empty())
53895388
return this->emitThis(E);
53905389

5391-
if (!InitStack.empty()) {
5392-
// If our init stack is, for example:
5393-
// 0 Stack: 3 (decl)
5394-
// 1 Stack: 6 (init list)
5395-
// 2 Stack: 1 (field)
5396-
// 3 Stack: 6 (init list)
5397-
// 4 Stack: 1 (field)
5398-
//
5399-
// We want to find the LAST element in it that's an init list,
5400-
// which is marked with the K_InitList marker. The index right
5401-
// before that points to an init list. We need to find the
5402-
// elements before the K_InitList element that point to a base
5403-
// (e.g. a decl or This), optionally followed by field, elem, etc.
5404-
// In the example above, we want to emit elements [0..2].
5405-
unsigned StartIndex = 0;
5406-
unsigned EndIndex = 0;
5407-
// Find the init list.
5408-
for (StartIndex = InitStack.size() - 1; StartIndex > 0; --StartIndex) {
5409-
if (InitStack[StartIndex].Kind == InitLink::K_InitList ||
5410-
InitStack[StartIndex].Kind == InitLink::K_This) {
5411-
EndIndex = StartIndex;
5412-
--StartIndex;
5413-
break;
5414-
}
5390+
// If our init stack is, for example:
5391+
// 0 Stack: 3 (decl)
5392+
// 1 Stack: 6 (init list)
5393+
// 2 Stack: 1 (field)
5394+
// 3 Stack: 6 (init list)
5395+
// 4 Stack: 1 (field)
5396+
//
5397+
// We want to find the LAST element in it that's an init list,
5398+
// which is marked with the K_InitList marker. The index right
5399+
// before that points to an init list. We need to find the
5400+
// elements before the K_InitList element that point to a base
5401+
// (e.g. a decl or This), optionally followed by field, elem, etc.
5402+
// In the example above, we want to emit elements [0..2].
5403+
unsigned StartIndex = 0;
5404+
unsigned EndIndex = 0;
5405+
// Find the init list.
5406+
for (StartIndex = InitStack.size() - 1; StartIndex > 0; --StartIndex) {
5407+
if (InitStack[StartIndex].Kind == InitLink::K_InitList ||
5408+
InitStack[StartIndex].Kind == InitLink::K_This) {
5409+
EndIndex = StartIndex;
5410+
--StartIndex;
5411+
break;
54155412
}
5413+
}
54165414

5417-
// Walk backwards to find the base.
5418-
for (; StartIndex > 0; --StartIndex) {
5419-
if (InitStack[StartIndex].Kind == InitLink::K_InitList)
5420-
continue;
5415+
// Walk backwards to find the base.
5416+
for (; StartIndex > 0; --StartIndex) {
5417+
if (InitStack[StartIndex].Kind == InitLink::K_InitList)
5418+
continue;
54215419

5422-
if (InitStack[StartIndex].Kind != InitLink::K_Field &&
5423-
InitStack[StartIndex].Kind != InitLink::K_Elem)
5424-
break;
5425-
}
5420+
if (InitStack[StartIndex].Kind != InitLink::K_Field &&
5421+
InitStack[StartIndex].Kind != InitLink::K_Elem)
5422+
break;
5423+
}
54265424

5427-
// Emit the instructions.
5428-
for (unsigned I = StartIndex; I != EndIndex; ++I) {
5429-
if (InitStack[I].Kind == InitLink::K_InitList)
5430-
continue;
5431-
if (!InitStack[I].template emit<Emitter>(this, E))
5432-
return false;
5433-
}
5434-
return true;
5425+
if (StartIndex == 0 && EndIndex == 0)
5426+
EndIndex = InitStack.size() - 1;
5427+
5428+
assert(StartIndex < EndIndex);
5429+
5430+
// Emit the instructions.
5431+
for (unsigned I = StartIndex; I != (EndIndex + 1); ++I) {
5432+
if (InitStack[I].Kind == InitLink::K_InitList)
5433+
continue;
5434+
if (!InitStack[I].template emit<Emitter>(this, E))
5435+
return false;
54355436
}
5436-
return this->emitThis(E);
5437+
return true;
54375438
}
54385439

54395440
template <class Emitter> bool Compiler<Emitter>::visitStmt(const Stmt *S) {
@@ -6295,6 +6296,10 @@ bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) {
62956296
}
62966297
assert(NestedField);
62976298

6299+
unsigned FirstLinkOffset =
6300+
R->getField(cast<FieldDecl>(IFD->chain()[0]))->Offset;
6301+
InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(InitExpr));
6302+
InitLinkScope<Emitter> ILS(this, InitLink::Field(FirstLinkOffset));
62986303
if (!emitFieldInitializer(NestedField, NestedFieldOffset, InitExpr,
62996304
IsUnion))
63006305
return false;

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,27 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
454454
assert(!cir::MissingFeatures::coroSizeBuiltinCall());
455455
return getUndefRValue(e->getType());
456456
}
457+
case Builtin::BI__builtin_prefetch: {
458+
auto evaluateOperandAsInt = [&](const Expr *arg) {
459+
Expr::EvalResult res;
460+
[[maybe_unused]] bool evalSucceed =
461+
arg->EvaluateAsInt(res, cgm.getASTContext());
462+
assert(evalSucceed && "expression should be able to evaluate as int");
463+
return res.Val.getInt().getZExtValue();
464+
};
465+
466+
bool isWrite = false;
467+
if (e->getNumArgs() > 1)
468+
isWrite = evaluateOperandAsInt(e->getArg(1));
469+
470+
int locality = 3;
471+
if (e->getNumArgs() > 2)
472+
locality = evaluateOperandAsInt(e->getArg(2));
473+
474+
mlir::Value address = emitScalarExpr(e->getArg(0));
475+
cir::PrefetchOp::create(builder, loc, address, locality, isWrite);
476+
return RValue::get(nullptr);
477+
}
457478
}
458479

459480
// If this is an alias for a lib function (e.g. __builtin_sin), emit

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,6 +1695,15 @@ static uint64_t getTypeSize(mlir::Type type, mlir::Operation &op) {
16951695
return llvm::divideCeil(layout.getTypeSizeInBits(type), 8);
16961696
}
16971697

1698+
mlir::LogicalResult CIRToLLVMPrefetchOpLowering::matchAndRewrite(
1699+
cir::PrefetchOp op, OpAdaptor adaptor,
1700+
mlir::ConversionPatternRewriter &rewriter) const {
1701+
rewriter.replaceOpWithNewOp<mlir::LLVM::Prefetch>(
1702+
op, adaptor.getAddr(), adaptor.getIsWrite(), adaptor.getLocality(),
1703+
/*DataCache=*/1);
1704+
return mlir::success();
1705+
}
1706+
16981707
mlir::LogicalResult CIRToLLVMPtrDiffOpLowering::matchAndRewrite(
16991708
cir::PtrDiffOp op, OpAdaptor adaptor,
17001709
mlir::ConversionPatternRewriter &rewriter) const {

0 commit comments

Comments
 (0)