Skip to content

[SYCL][FPGA] Enable a set of loop attributes #1312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,57 @@ def SYCLIntelFPGAMaxConcurrency : Attr {
let Documentation = [SYCLIntelFPGAMaxConcurrencyAttrDocs];
}

def SYCLIntelFPGALoopCoalesce : Attr {
let Spellings = [CXX11<"intelfpga","loop_coalesce">];
let Args = [ExprArgument<"NExpr">];
let LangOpts = [SYCLIsDevice, SYCLIsHost];
let HasCustomTypeTransform = 1;
let AdditionalMembers = [{
static const char *getName() {
return "loop_coalesce";
}
}];
let Documentation = [SYCLIntelFPGALoopCoalesceAttrDocs];
}

def SYCLIntelFPGADisableLoopPipelining : Attr {
let Spellings = [CXX11<"intelfpga","disable_loop_pipelining">];
let LangOpts = [SYCLIsDevice, SYCLIsHost];
let HasCustomTypeTransform = 1;
let AdditionalMembers = [{
static const char *getName() {
return "disable_loop_pipelining";
}
}];
let Documentation = [SYCLIntelFPGADisableLoopPipeliningAttrDocs];
}

def SYCLIntelFPGAMaxInterleaving : Attr {
let Spellings = [CXX11<"intelfpga","max_interleaving">];
let Args = [ExprArgument<"NExpr">];
let LangOpts = [SYCLIsDevice, SYCLIsHost];
let HasCustomTypeTransform = 1;
let AdditionalMembers = [{
static const char *getName() {
return "max_interleaving";
}
}];
let Documentation = [SYCLIntelFPGAMaxInterleavingAttrDocs];
}

def SYCLIntelFPGASpeculatedIterations : Attr {
let Spellings = [CXX11<"intelfpga","speculated_iterations">];
let Args = [ExprArgument<"NExpr">];
let LangOpts = [SYCLIsDevice, SYCLIsHost];
let HasCustomTypeTransform = 1;
let AdditionalMembers = [{
static const char *getName() {
return "speculated_iterations";
}
}];
let Documentation = [SYCLIntelFPGASpeculatedIterationsAttrDocs];
}

def IntelFPGALocalNonConstVar : SubsetSubject<Var,
[{S->hasLocalStorage() &&
S->getKind() != Decl::ImplicitParam &&
Expand Down
47 changes: 47 additions & 0 deletions clang/include/clang/Basic/AttrDocs.td
Original file line number Diff line number Diff line change
Expand Up @@ -2090,6 +2090,53 @@ be applied multiple times to the same loop.
}];
}

def SYCLIntelFPGALoopCoalesceAttrDocs : Documentation {
let Category = DocCatVariable;
let Heading = "loop_coalesce";
let Content = [{
This attribute applies to a loop. Indicates that the loop nest should be
coalesced into a single loop without affecting functionality. Parameter N is
optional. If specified, it shall be a positive integer, and indicates how many
of the nested loop levels should be coalesced.
}];
}

def SYCLIntelFPGADisableLoopPipeliningAttrDocs : Documentation {
let Category = DocCatVariable;
let Heading = "disable_loop_pipelining";
let Content = [{
This attribute applies to a loop. Disables pipelining of the loop data path,
causing the loop to be executed serially. Cannot be used on the same loop in
conjunction with max_interleaving, speculated_iterations, max_concurrency, ii
or ivdep.
}];
}

def SYCLIntelFPGAMaxInterleavingAttrDocs : Documentation {
let Category = DocCatVariable;
let Heading = "max_interleaving";
let Content = [{
This attribute applies to a loop. Places a maximum limit N on the number of
interleaved invocations of an inner loop by an outer loop (note, this does not
mean that this attribute can only be applied to inner loops in user code - outer
loops in user code may still be contained in an implicit loop due to NDRange).
Parameter N is mandatory, and shall be non-negative integer. Cannot be
used on the same loop in conjunction with disable_loop_pipelining.
}];
}

def SYCLIntelFPGASpeculatedIterationsAttrDocs : Documentation {
let Category = DocCatVariable;
let Heading = "speculated_iterations";
let Content = [{
This attribute applies to a loop. Specifies the number of concurrent speculated
iterations that will be in flight for a loop invocation (i.e. the exit
condition for these iterations will not have been evaluated yet).
Parameter N is mandatory, and may either be 0, or a positive integer. Cannot be
used on the same loop in conjunction with disable_loop_pipelining.
}];
}

def SYCLDeviceIndirectlyCallableDocs : Documentation {
let Category = DocCatFunction;
let Heading = "intel::device_indirectly_callable";
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -1719,7 +1719,7 @@ class Sema final {
Expr *Expr2);
template <typename FPGALoopAttrT>
FPGALoopAttrT *BuildSYCLIntelFPGALoopAttr(const AttributeCommonInfo &A,
Expr *E);
Expr *E = nullptr);

LoopUnrollHintAttr *BuildLoopUnrollHintAttr(const AttributeCommonInfo &A,
Expr *E);
Expand Down
134 changes: 124 additions & 10 deletions clang/lib/CodeGen/CGLoopInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,6 @@ MDNode *LoopInfo::createMetadata(

// Setting ii attribute with an initiation interval
if (Attrs.SYCLIInterval > 0) {
LLVMContext &Ctx = Header->getContext();
Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.ii.count"),
ConstantAsMetadata::get(ConstantInt::get(
llvm::Type::getInt32Ty(Ctx), Attrs.SYCLIInterval))};
Expand All @@ -516,14 +515,52 @@ MDNode *LoopInfo::createMetadata(

// Setting max_concurrency attribute with number of threads
if (Attrs.SYCLMaxConcurrencyEnable) {
LLVMContext &Ctx = Header->getContext();
Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.max_concurrency.count"),
ConstantAsMetadata::get(ConstantInt::get(
llvm::Type::getInt32Ty(Ctx),
Attrs.SYCLMaxConcurrencyNThreads))};
LoopProperties.push_back(MDNode::get(Ctx, Vals));
}

if (Attrs.SYCLLoopCoalesceEnable) {
Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.coalesce.enable")};
LoopProperties.push_back(MDNode::get(Ctx, Vals));
}

if (Attrs.SYCLLoopCoalesceNLevels > 0) {
Metadata *Vals[] = {
MDString::get(Ctx, "llvm.loop.coalesce.count"),
ConstantAsMetadata::get(ConstantInt::get(
llvm::Type::getInt32Ty(Ctx), Attrs.SYCLLoopCoalesceNLevels))};
LoopProperties.push_back(MDNode::get(Ctx, Vals));
}

// disable_loop_pipelining attribute corresponds to
// 'llvm.loop.intel.pipelining.enable, i32 0' metadata
if (Attrs.SYCLLoopPipeliningDisable) {
Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.intel.pipelining.enable"),
ConstantAsMetadata::get(
ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 0))};
LoopProperties.push_back(MDNode::get(Ctx, Vals));
}

if (Attrs.SYCLMaxInterleavingEnable) {
Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.max_interleaving.count"),
ConstantAsMetadata::get(ConstantInt::get(
llvm::Type::getInt32Ty(Ctx),
Attrs.SYCLMaxInterleavingNInvocations))};
LoopProperties.push_back(MDNode::get(Ctx, Vals));
}

if (Attrs.SYCLSpeculatedIterationsEnable) {
Metadata *Vals[] = {
MDString::get(Ctx, "llvm.loop.intel.speculated.iterations.count"),
ConstantAsMetadata::get(
ConstantInt::get(llvm::Type::getInt32Ty(Ctx),
Attrs.SYCLSpeculatedIterationsNIterations))};
LoopProperties.push_back(MDNode::get(Ctx, Vals));
}

LoopProperties.insert(LoopProperties.end(), AdditionalLoopProperties.begin(),
AdditionalLoopProperties.end());
return createFullUnrollMetadata(Attrs, LoopProperties, HasUserTransforms);
Expand All @@ -535,9 +572,13 @@ LoopAttributes::LoopAttributes(bool IsParallel)
UnrollAndJamEnable(LoopAttributes::Unspecified),
VectorizePredicateEnable(LoopAttributes::Unspecified), VectorizeWidth(0),
InterleaveCount(0), SYCLIInterval(0), SYCLMaxConcurrencyEnable(false),
SYCLMaxConcurrencyNThreads(0), UnrollCount(0), UnrollAndJamCount(0),
DistributeEnable(LoopAttributes::Unspecified), PipelineDisabled(false),
PipelineInitiationInterval(0) {}
SYCLMaxConcurrencyNThreads(0), SYCLLoopCoalesceEnable(false),
SYCLLoopCoalesceNLevels(0), SYCLLoopPipeliningDisable(false),
SYCLMaxInterleavingEnable(false), SYCLMaxInterleavingNInvocations(0),
SYCLSpeculatedIterationsEnable(false),
SYCLSpeculatedIterationsNIterations(0), UnrollCount(0),
UnrollAndJamCount(0), DistributeEnable(LoopAttributes::Unspecified),
PipelineDisabled(false), PipelineInitiationInterval(0) {}

void LoopAttributes::clear() {
IsParallel = false;
Expand All @@ -547,6 +588,13 @@ void LoopAttributes::clear() {
SYCLIInterval = 0;
SYCLMaxConcurrencyEnable = false;
SYCLMaxConcurrencyNThreads = 0;
SYCLLoopCoalesceEnable = false;
SYCLLoopCoalesceNLevels = 0;
SYCLLoopPipeliningDisable = false;
SYCLMaxInterleavingEnable = false;
SYCLMaxInterleavingNInvocations = 0;
SYCLSpeculatedIterationsEnable = false;
SYCLSpeculatedIterationsNIterations = 0;
InterleaveCount = 0;
UnrollCount = 0;
UnrollAndJamCount = 0;
Expand Down Expand Up @@ -574,9 +622,16 @@ LoopInfo::LoopInfo(BasicBlock *Header, const LoopAttributes &Attrs,
if (!Attrs.IsParallel && Attrs.VectorizeWidth == 0 &&
Attrs.InterleaveCount == 0 && !Attrs.GlobalSYCLIVDepInfo.hasValue() &&
Attrs.ArraySYCLIVDepInfo.empty() && Attrs.SYCLIInterval == 0 &&
Attrs.SYCLMaxConcurrencyEnable == false && Attrs.UnrollCount == 0 &&
Attrs.UnrollAndJamCount == 0 && !Attrs.PipelineDisabled &&
Attrs.PipelineInitiationInterval == 0 &&
Attrs.SYCLMaxConcurrencyEnable == false &&
Attrs.SYCLLoopCoalesceEnable == false &&
Attrs.SYCLLoopCoalesceNLevels == 0 &&
Attrs.SYCLLoopPipeliningDisable == false &&
Attrs.SYCLMaxInterleavingEnable == false &&
Attrs.SYCLMaxInterleavingNInvocations == 0 &&
Attrs.SYCLSpeculatedIterationsEnable == false &&
Attrs.SYCLSpeculatedIterationsNIterations == 0 &&
Attrs.UnrollCount == 0 && Attrs.UnrollAndJamCount == 0 &&
!Attrs.PipelineDisabled && Attrs.PipelineInitiationInterval == 0 &&
Attrs.VectorizePredicateEnable == LoopAttributes::Unspecified &&
Attrs.VectorizeEnable == LoopAttributes::Unspecified &&
Attrs.UnrollEnable == LoopAttributes::Unspecified &&
Expand Down Expand Up @@ -877,15 +932,36 @@ void LoopInfoStack::push(BasicBlock *Header, clang::ASTContext &Ctx,
// n - 'llvm.loop.ii.count, i32 n' metadata will be emitted
// For attribute max_concurrency:
// n - 'llvm.loop.max_concurrency.count, i32 n' metadata will be emitted
// For attribute loop_coalesce:
// without parameter - 'lvm.loop.coalesce.enable' metadata will be emitted
// n - 'llvm.loop.coalesce.count, i32 n' metadata will be emitted
// For attribute disable_loop_pipelining:
// 'llvm.loop.intel.pipelining.enable, i32 0' metadata will be emitted
// For attribute max_interleaving:
// n - 'llvm.loop.max_interleaving.count, i32 n' metadata will be emitted
// For attribute speculated_iterations:
// n - 'llvm.loop.intel.speculated.iterations.count, i32 n' metadata will be
// emitted
for (const auto *Attr : Attrs) {
const SYCLIntelFPGAIVDepAttr *IntelFPGAIVDep =
dyn_cast<SYCLIntelFPGAIVDepAttr>(Attr);
const SYCLIntelFPGAIIAttr *IntelFPGAII =
dyn_cast<SYCLIntelFPGAIIAttr>(Attr);
const SYCLIntelFPGAMaxConcurrencyAttr *IntelFPGAMaxConcurrency =
dyn_cast<SYCLIntelFPGAMaxConcurrencyAttr>(Attr);

if (!IntelFPGAIVDep && !IntelFPGAII && !IntelFPGAMaxConcurrency)
const SYCLIntelFPGALoopCoalesceAttr *IntelFPGALoopCoalesce =
dyn_cast<SYCLIntelFPGALoopCoalesceAttr>(Attr);
const SYCLIntelFPGADisableLoopPipeliningAttr
*IntelFPGADisableLoopPipelining =
dyn_cast<SYCLIntelFPGADisableLoopPipeliningAttr>(Attr);
const SYCLIntelFPGAMaxInterleavingAttr *IntelFPGAMaxInterleaving =
dyn_cast<SYCLIntelFPGAMaxInterleavingAttr>(Attr);
const SYCLIntelFPGASpeculatedIterationsAttr *IntelFPGASpeculatedIterations =
dyn_cast<SYCLIntelFPGASpeculatedIterationsAttr>(Attr);

if (!IntelFPGAIVDep && !IntelFPGAII && !IntelFPGAMaxConcurrency &&
!IntelFPGALoopCoalesce && !IntelFPGADisableLoopPipelining &&
!IntelFPGAMaxInterleaving && !IntelFPGASpeculatedIterations)
continue;

if (IntelFPGAIVDep) {
Expand Down Expand Up @@ -918,6 +994,44 @@ void LoopInfoStack::push(BasicBlock *Header, clang::ASTContext &Ctx,
setSYCLMaxConcurrencyEnable();
setSYCLMaxConcurrencyNThreads(ArgVal.getSExtValue());
}

if (IntelFPGALoopCoalesce) {
llvm::APSInt ArgVal(32);
if (auto *LCE = IntelFPGALoopCoalesce->getNExpr()) {
bool IsValid = LCE->isIntegerConstantExpr(ArgVal, Ctx);
assert(IsValid && "Not an integer constant expression");
(void)IsValid;
setSYCLLoopCoalesceNLevels(ArgVal.getSExtValue());
} else {
setSYCLLoopCoalesceEnable();
}
}

if (IntelFPGADisableLoopPipelining) {
setSYCLLoopPipeliningDisable();
}

if (IntelFPGAMaxInterleaving) {
llvm::APSInt ArgVal(32);
bool IsValid =
IntelFPGAMaxInterleaving->getNExpr()->isIntegerConstantExpr(ArgVal,
Ctx);
assert(IsValid && "Not an integer constant expression");
(void)IsValid;
setSYCLMaxInterleavingEnable();
setSYCLMaxInterleavingNInvocations(ArgVal.getSExtValue());
}

if (IntelFPGASpeculatedIterations) {
llvm::APSInt ArgVal(32);
bool IsValid =
IntelFPGASpeculatedIterations->getNExpr()->isIntegerConstantExpr(
ArgVal, Ctx);
assert(IsValid && "Not an integer constant expression");
(void)IsValid;
setSYCLSpeculatedIterationsEnable();
setSYCLSpeculatedIterationsNIterations(ArgVal.getSExtValue());
}
}

/// Stage the attributes.
Expand Down
56 changes: 56 additions & 0 deletions clang/lib/CodeGen/CGLoopInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,27 @@ struct LoopAttributes {
/// Value for llvm.loop.max_concurrency.count metadata.
unsigned SYCLMaxConcurrencyNThreads;

/// Flag for llvm.loop.coalesce metadata.
bool SYCLLoopCoalesceEnable;

/// Value for llvm.loop.coalesce.count metadata.
unsigned SYCLLoopCoalesceNLevels;

/// Flag for llvm.loop.intel.pipelining.enable, i32 0 metadata.
bool SYCLLoopPipeliningDisable;

/// Flag for llvm.loop.max_interleaving.count metadata.
bool SYCLMaxInterleavingEnable;

/// Value for llvm.loop.max_interleaving.count metadata.
unsigned SYCLMaxInterleavingNInvocations;

/// Flag for llvm.loop.intel.speculated.iterations.count metadata.
bool SYCLSpeculatedIterationsEnable;

/// Value for llvm.loop.intel.speculated.iterations.count metadata.
unsigned SYCLSpeculatedIterationsNIterations;

/// llvm.unroll.
unsigned UnrollCount;

Expand Down Expand Up @@ -333,6 +354,41 @@ class LoopInfoStack {
StagedAttrs.SYCLMaxConcurrencyNThreads = C;
}

/// Set flag of loop_coalesce for the next loop pushed.
void setSYCLLoopCoalesceEnable() {
StagedAttrs.SYCLLoopCoalesceEnable = true;
}

/// Set value of coalesced levels for the next loop pushed.
void setSYCLLoopCoalesceNLevels(unsigned C) {
StagedAttrs.SYCLLoopCoalesceNLevels = C;
}

/// Set flag of disable_loop_pipelining for the next loop pushed.
void setSYCLLoopPipeliningDisable() {
StagedAttrs.SYCLLoopPipeliningDisable = true;
}

/// Set flag of max_interleaving for the next loop pushed.
void setSYCLMaxInterleavingEnable() {
StagedAttrs.SYCLMaxInterleavingEnable = true;
}

/// Set value of max interleaved invocations for the next loop pushed.
void setSYCLMaxInterleavingNInvocations(unsigned C) {
StagedAttrs.SYCLMaxInterleavingNInvocations = C;
}

/// Set flag of speculated_iterations for the next loop pushed.
void setSYCLSpeculatedIterationsEnable() {
StagedAttrs.SYCLSpeculatedIterationsEnable = true;
}

/// Set value of concurrent speculated iterations for the next loop pushed.
void setSYCLSpeculatedIterationsNIterations(unsigned C) {
StagedAttrs.SYCLSpeculatedIterationsNIterations = C;
}

/// Set the unroll count for the next loop pushed.
void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; }

Expand Down
Loading