-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spec: microsoft/hlsl-specs#263 - `Attr.td` - Define the HLSL loop attribute hints (unroll and loop) - `AttrDocs.td` - Add documentation for unroll and loop - `CGLoopInfo.cpp` - Add codegen for HLSL unroll that maps to clang unroll expectations - `ParseStmt.cpp` - For statements if HLSL define DeclSpecAttrs via MaybeParseMicrosoftAttributes - `SemaStmtAttr.cpp` - Add the HLSL loop unroll handeling resolves #70114 dxc examples: - for loop: https://hlsl.godbolt.org/z/8EK6Pa139 - while loop: https://hlsl.godbolt.org/z/ebr5MvEcK - do while: https://hlsl.godbolt.org/z/be8cedoTs Documentation: ![Screenshot_20240531_143000](https://github.com/llvm/llvm-project/assets/1802579/9da9df9b-68a6-49eb-9d4f-e080aa2eff7f)
- Loading branch information
Showing
7 changed files
with
343 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4172,6 +4172,18 @@ def LoopHint : Attr { | |
let HasCustomParsing = 1; | ||
} | ||
|
||
/// The HLSL loop attributes | ||
def HLSLLoopHint: StmtAttr { | ||
/// [unroll(directive)] | ||
/// [loop] | ||
let Spellings = [Microsoft<"unroll">, Microsoft<"loop">]; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
AaronBallman
Collaborator
|
||
let Args = [UnsignedArgument<"directive", /*opt*/1>]; | ||
let Subjects = SubjectList<[ForStmt, WhileStmt, DoStmt], | ||
ErrorDiag, "'for', 'while', and 'do' statements">; | ||
let LangOpts = [HLSL]; | ||
let Documentation = [HLSLLoopHintDocs, HLSLUnrollHintDocs]; | ||
} | ||
|
||
def CapturedRecord : InheritableAttr { | ||
// This attribute has no spellings as it is only ever created implicitly. | ||
let Spellings = []; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \ | ||
// RUN: dxil-pc-shadermodel6.3-library -disable-llvm-passes %s -emit-llvm -o - | FileCheck %s | ||
|
||
/*** for ***/ | ||
void for_count() | ||
{ | ||
// CHECK-LABEL: for_count | ||
[unroll(8)] | ||
for( int i = 0; i < 1000; ++i); | ||
// CHECK: br label %{{.*}}, !llvm.loop ![[FOR_DISTINCT:.*]] | ||
} | ||
|
||
void for_disable() | ||
{ | ||
// CHECK-LABEL: for_disable | ||
[loop] | ||
for( int i = 0; i < 1000; ++i); | ||
// CHECK: br label %{{.*}}, !llvm.loop ![[FOR_DISABLE:.*]] | ||
} | ||
|
||
void for_enable() | ||
{ | ||
// CHECK-LABEL: for_enable | ||
[unroll] | ||
for( int i = 0; i < 1000; ++i); | ||
// CHECK: br label %{{.*}}, !llvm.loop ![[FOR_ENABLE:.*]] | ||
} | ||
|
||
void for_nested_one_unroll_enable() | ||
{ | ||
// CHECK-LABEL: for_nested_one_unroll_enable | ||
int s = 0; | ||
[unroll] | ||
for( int i = 0; i < 1000; ++i) { | ||
for( int j = 0; j < 10; ++j) | ||
s += i + j; | ||
} | ||
// CHECK: br label %{{.*}}, !llvm.loop ![[FOR_NESTED_ENABLE:.*]] | ||
// CHECK-NOT: br label %{{.*}}, !llvm.loop ![[FOR_NESTED_1_ENABLE:.*]] | ||
} | ||
|
||
void for_nested_two_unroll_enable() | ||
{ | ||
// CHECK-LABEL: for_nested_two_unroll_enable | ||
int s = 0; | ||
[unroll] | ||
for( int i = 0; i < 1000; ++i) { | ||
[unroll] | ||
for( int j = 0; j < 10; ++j) | ||
s += i + j; | ||
} | ||
// CHECK: br label %{{.*}}, !llvm.loop ![[FOR_NESTED2_ENABLE:.*]] | ||
// CHECK: br label %{{.*}}, !llvm.loop ![[FOR_NESTED2_1_ENABLE:.*]] | ||
} | ||
|
||
|
||
/*** while ***/ | ||
void while_count() | ||
{ | ||
// CHECK-LABEL: while_count | ||
int i = 1000; | ||
[unroll(4)] | ||
while(i-->0); | ||
// CHECK: br label %{{.*}}, !llvm.loop ![[WHILE_DISTINCT:.*]] | ||
} | ||
|
||
void while_disable() | ||
{ | ||
// CHECK-LABEL: while_disable | ||
int i = 1000; | ||
[loop] | ||
while(i-->0); | ||
// CHECK: br label %{{.*}}, !llvm.loop ![[WHILE_DISABLE:.*]] | ||
} | ||
|
||
void while_enable() | ||
{ | ||
// CHECK-LABEL: while_enable | ||
int i = 1000; | ||
[unroll] | ||
while(i-->0); | ||
// CHECK: br label %{{.*}}, !llvm.loop ![[WHILE_ENABLE:.*]] | ||
} | ||
|
||
/*** do ***/ | ||
void do_count() | ||
{ | ||
// CHECK-LABEL: do_count | ||
int i = 1000; | ||
[unroll(16)] | ||
do {} while(i--> 0); | ||
// CHECK: br i1 %{{.*}}, label %{{.*}}, label %{{.*}}, !llvm.loop ![[DO_DISTINCT:.*]] | ||
} | ||
|
||
void do_disable() | ||
{ | ||
// CHECK-LABEL: do_disable | ||
int i = 1000; | ||
[loop] | ||
do {} while(i--> 0); | ||
// CHECK: br i1 %{{.*}}, label %{{.*}}, label %{{.*}}, !llvm.loop ![[DO_DISABLE:.*]] | ||
} | ||
|
||
void do_enable() | ||
{ | ||
// CHECK-LABEL: do_enable | ||
int i = 1000; | ||
[unroll] | ||
do {} while(i--> 0); | ||
// CHECK: br i1 %{{.*}}, label %{{.*}}, label %{{.*}}, !llvm.loop ![[DO_ENABLE:.*]] | ||
} | ||
|
||
|
||
// CHECK: ![[FOR_DISTINCT]] = distinct !{![[FOR_DISTINCT]], ![[FOR_COUNT:.*]]} | ||
// CHECK: ![[FOR_COUNT]] = !{!"llvm.loop.unroll.count", i32 8} | ||
// CHECK: ![[FOR_DISABLE]] = distinct !{![[FOR_DISABLE]], ![[DISABLE:.*]]} | ||
// CHECK: ![[DISABLE]] = !{!"llvm.loop.unroll.disable"} | ||
// CHECK: ![[FOR_ENABLE]] = distinct !{![[FOR_ENABLE]], ![[ENABLE:.*]]} | ||
// CHECK: ![[ENABLE]] = !{!"llvm.loop.unroll.enable"} | ||
// CHECK: ![[FOR_NESTED_ENABLE]] = distinct !{![[FOR_NESTED_ENABLE]], ![[ENABLE]]} | ||
// CHECK: ![[FOR_NESTED2_ENABLE]] = distinct !{![[FOR_NESTED2_ENABLE]], ![[ENABLE]]} | ||
// CHECK: ![[FOR_NESTED2_1_ENABLE]] = distinct !{![[FOR_NESTED2_1_ENABLE]], ![[ENABLE]]} | ||
// CHECK: ![[WHILE_DISTINCT]] = distinct !{![[WHILE_DISTINCT]], ![[WHILE_COUNT:.*]]} | ||
// CHECK: ![[WHILE_COUNT]] = !{!"llvm.loop.unroll.count", i32 4} | ||
// CHECK: ![[WHILE_DISABLE]] = distinct !{![[WHILE_DISABLE]], ![[DISABLE]]} | ||
// CHECK: ![[WHILE_ENABLE]] = distinct !{![[WHILE_ENABLE]], ![[ENABLE]]} | ||
// CHECK: ![[DO_DISTINCT]] = distinct !{![[DO_DISTINCT]], ![[DO_COUNT:.*]]} | ||
// CHECK: ![[DO_COUNT]] = !{!"llvm.loop.unroll.count", i32 16} | ||
// CHECK: ![[DO_DISABLE]] = distinct !{![[DO_DISABLE]], ![[DISABLE]]} | ||
// CHECK: ![[DO_ENABLE]] = distinct !{![[DO_ENABLE]], ![[ENABLE]]} |
Oops, something went wrong.
It seems this change is causing a problem in attributeHasIdentifierArg. This is supposed to return true when the first Arg is an Identifier. It returns true for this one when it is not an Identifier. This is because the tablegen (see CLANG_ATTR_IDENTIFIER_ARG_LIST) matches only the name "unroll" and the LoopHint unroll is making it return true. So the call to A.isArgIdent(0) should be false for something like unroll(I) with the Microsoft spelling. I think that's what's going on anyway. We've changed this in our downstream to check a 'fullname' with scope and syntax added. Do we need to do this upstream now to handle this properly? @farzonl @AaronBallman