diff options
author | Aaron Ballman <aaron@aaronballman.com> | 2014-06-06 12:40:24 +0000 |
---|---|---|
committer | Aaron Ballman <aaron@aaronballman.com> | 2014-06-06 12:40:24 +0000 |
commit | b06b15aa28d0b2efce5a2eb58ae0a73fa1ffe94d (patch) | |
tree | e0d0c2cd0c6a64f5cb212503bf62f3062c9e0ff9 | |
parent | 909b7490a338fac18c59317c48d970c50bcb6552 (diff) | |
download | bcm5719-llvm-b06b15aa28d0b2efce5a2eb58ae0a73fa1ffe94d.tar.gz bcm5719-llvm-b06b15aa28d0b2efce5a2eb58ae0a73fa1ffe94d.zip |
Adding a new #pragma for the vectorize and interleave optimization hints.
Patch thanks to Tyler Nowicki!
llvm-svn: 210330
-rw-r--r-- | clang/include/clang/Basic/Attr.td | 53 | ||||
-rw-r--r-- | clang/include/clang/Basic/DiagnosticParseKinds.td | 16 | ||||
-rw-r--r-- | clang/include/clang/Basic/DiagnosticSemaKinds.td | 21 | ||||
-rw-r--r-- | clang/include/clang/Basic/TokenKinds.def | 5 | ||||
-rw-r--r-- | clang/include/clang/Parse/Parser.h | 9 | ||||
-rw-r--r-- | clang/include/clang/Sema/LoopHint.h | 31 | ||||
-rw-r--r-- | clang/lib/AST/StmtPrinter.cpp | 18 | ||||
-rw-r--r-- | clang/lib/CodeGen/CGStmt.cpp | 136 | ||||
-rw-r--r-- | clang/lib/CodeGen/CodeGenFunction.h | 14 | ||||
-rw-r--r-- | clang/lib/Parse/ParsePragma.cpp | 220 | ||||
-rw-r--r-- | clang/lib/Parse/ParseStmt.cpp | 37 | ||||
-rw-r--r-- | clang/lib/Sema/SemaStmtAttr.cpp | 205 | ||||
-rw-r--r-- | clang/test/CodeGen/pragma-loop.cpp | 120 | ||||
-rw-r--r-- | clang/test/PCH/pragma-loop.cpp | 62 | ||||
-rw-r--r-- | clang/test/Parser/pragma-loop.cpp | 132 |
15 files changed, 986 insertions, 93 deletions
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index 11a30b9010d..3556ea437fb 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -1757,6 +1757,53 @@ def MSVtorDisp : InheritableAttr { let Documentation = [Undocumented]; } -def Unaligned : IgnoredAttr { - let Spellings = [Keyword<"__unaligned">]; -} +def Unaligned : IgnoredAttr {
+ let Spellings = [Keyword<"__unaligned">];
+}
+
+def LoopHint : Attr {
+ /// vectorize: vectorizes loop operations if 'value != 0'.
+ /// vectorize_width: vectorize loop operations with width 'value'.
+ /// interleave: interleave multiple loop iterations if 'value != 0'.
+ /// interleave_count: interleaves 'value' loop interations.
+
+ /// FIXME: Add Pragma spelling to tablegen and
+ /// use it here.
+ let Spellings = [Keyword<"loop">];
+
+ /// State of the loop optimization specified by the spelling.
+ let Args = [EnumArgument<"Option", "OptionType",
+ ["vectorize", "vectorize_width", "interleave", "interleave_count"],
+ ["Vectorize", "VectorizeWidth", "Interleave", "InterleaveCount"]>,
+ DefaultIntArgument<"Value", 1>];
+
+ let AdditionalMembers = [{
+ static StringRef getOptionName(int Option) {
+ switch(Option) {
+ case Vectorize: return "vectorize";
+ case VectorizeWidth: return "vectorize_width";
+ case Interleave: return "interleave";
+ case InterleaveCount: return "interleave_count";
+ }
+ llvm_unreachable("Unhandled LoopHint option.");
+ }
+
+ static StringRef getValueName(int Value) {
+ if (Value)
+ return "enable";
+ return "disable";
+ }
+
+ // FIXME: Modify pretty printer to print this pragma.
+ void print(raw_ostream &OS, const PrintingPolicy &Policy) const {
+ OS << "#pragma clang loop " << getOptionName(option) << "(";
+ if (option == VectorizeWidth || option == InterleaveCount)
+ OS << value;
+ else
+ OS << getValueName(value);
+ OS << ")\n";
+ }
+ }];
+
+ let Documentation = [Undocumented];
+}
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td index 6a01dfcbb6f..2ab5021fdc4 100644 --- a/clang/include/clang/Basic/DiagnosticParseKinds.td +++ b/clang/include/clang/Basic/DiagnosticParseKinds.td @@ -889,12 +889,16 @@ def err_omp_unexpected_directive : Error< def err_omp_expected_punc : Error< "expected ',' or ')' in '%0' clause">; def err_omp_unexpected_clause : Error< - "unexpected OpenMP clause '%0' in directive '#pragma omp %1'">; -def err_omp_more_one_clause : Error< - "directive '#pragma omp %0' cannot contain more than one '%1' clause">; -} // end of Parse Issue category. - -let CategoryName = "Modules Issue" in { + "unexpected OpenMP clause '%0' in directive '#pragma omp %1'">;
+def err_omp_more_one_clause : Error<
+ "directive '#pragma omp %0' cannot contain more than one '%1' clause">;
+
+// Pragma loop support.
+def err_pragma_loop_invalid_option : Error<
+ "%select{invalid|missing}0 option%select{ %1|}0; expected vectorize, vectorize_width, interleave, or interleave_count">;
+} // end of Parse Issue category.
+
+let CategoryName = "Modules Issue" in {
def err_module_expected_ident : Error< "expected a module name after module import">; def err_module_expected_semi : Error< diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 41c2100f795..f49ff18b86f 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -539,12 +539,21 @@ def err_pragma_push_visibility_mismatch : Error< def note_surrounding_namespace_ends_here : Note< "surrounding namespace with visibility attribute ends here">; def err_pragma_pop_visibility_mismatch : Error< - "#pragma visibility pop with no matching #pragma visibility push">; -def note_surrounding_namespace_starts_here : Note< - "surrounding namespace with visibility attribute starts here">; - -/// Objective-C parser diagnostics -def err_duplicate_class_def : Error< + "#pragma visibility pop with no matching #pragma visibility push">;
+def note_surrounding_namespace_starts_here : Note<
+ "surrounding namespace with visibility attribute starts here">;
+def err_pragma_loop_invalid_value : Error<
+ "%select{invalid|missing}0 value%select{ %1|}0; expected a positive integer value">;
+def err_pragma_loop_invalid_keyword : Error<
+ "%select{invalid|missing}0 keyword%select{ %1|}0; expected 'enable' or 'disable'">;
+def err_pragma_loop_compatibility : Error<
+ "%select{incompatible|duplicate}0 directives '%1(%2)' and '%3(%4)'">;
+def err_pragma_loop_precedes_nonloop : Error<
+ "expected a for, while, or do-while loop to follow the '#pragma clang loop' "
+ "directive">;
+
+/// Objective-C parser diagnostics
+def err_duplicate_class_def : Error<
"duplicate interface definition for class %0">; def err_undef_superclass : Error< "cannot find interface declaration for %0, superclass of %1">; diff --git a/clang/include/clang/Basic/TokenKinds.def b/clang/include/clang/Basic/TokenKinds.def index 845a8b096f6..5d088336dfe 100644 --- a/clang/include/clang/Basic/TokenKinds.def +++ b/clang/include/clang/Basic/TokenKinds.def @@ -701,6 +701,11 @@ ANNOTATION(pragma_opencl_extension) ANNOTATION(pragma_openmp) ANNOTATION(pragma_openmp_end) +// Annotations for loop pragma directives #pragma clang loop ... +// The lexer produces these so that they only take effect when the parser +// handles #pragma loop ... directives. +ANNOTATION(pragma_loop_hint) + // Annotations for module import translated from #include etc. ANNOTATION(module_include) ANNOTATION(module_begin) diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h index c2aa1e6df52..049a462bccf 100644 --- a/clang/include/clang/Parse/Parser.h +++ b/clang/include/clang/Parse/Parser.h @@ -20,6 +20,7 @@ #include "clang/Lex/CodeCompletionHandler.h" #include "clang/Lex/Preprocessor.h" #include "clang/Sema/DeclSpec.h" +#include "clang/Sema/LoopHint.h" #include "clang/Sema/Sema.h" #include "llvm/ADT/SmallVector.h" #include "llvm/Support/Compiler.h" @@ -161,6 +162,7 @@ class Parser : public CodeCompletionHandler { std::unique_ptr<PragmaHandler> MSCodeSeg; std::unique_ptr<PragmaHandler> MSSection; std::unique_ptr<PragmaHandler> OptimizeHandler; + std::unique_ptr<PragmaHandler> LoopHintHandler; std::unique_ptr<CommentHandler> CommentSemaHandler; @@ -519,6 +521,10 @@ private: /// #pragma clang __debug captured StmtResult HandlePragmaCaptured(); + /// \brief Handle the annotation token produced for + /// #pragma vectorize... + LoopHint HandlePragmaLoopHint(); + /// GetLookAheadToken - This peeks ahead N tokens and returns that token /// without consuming any tokens. LookAhead(0) returns 'Tok', LookAhead(1) /// returns the token after Tok, etc. @@ -1601,6 +1607,9 @@ private: StmtResult ParseReturnStatement(); StmtResult ParseAsmStatement(bool &msAsm); StmtResult ParseMicrosoftAsmStatement(SourceLocation AsmLoc); + StmtResult ParsePragmaLoopHint(StmtVector &Stmts, bool OnlyStatement, + SourceLocation *TrailingElseLoc, + ParsedAttributesWithRange &Attrs); /// \brief Describes the behavior that should be taken for an __if_exists /// block. diff --git a/clang/include/clang/Sema/LoopHint.h b/clang/include/clang/Sema/LoopHint.h new file mode 100644 index 00000000000..ad9b07d34a8 --- /dev/null +++ b/clang/include/clang/Sema/LoopHint.h @@ -0,0 +1,31 @@ +//===--- LoopHint.h - Types for LoopHint ------------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_SEMA_LOOPHINT_H
+#define LLVM_CLANG_SEMA_LOOPHINT_H
+
+#include "clang/Basic/IdentifierTable.h"
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Sema/AttributeList.h"
+#include "clang/Sema/Ownership.h"
+
+namespace clang {
+
+/// \brief Loop hint specified by a pragma loop directive.
+struct LoopHint {
+ SourceRange Range;
+ Expr *ValueExpr;
+ IdentifierLoc *LoopLoc;
+ IdentifierLoc *ValueLoc;
+ IdentifierLoc *OptionLoc;
+};
+
+} // end namespace clang
+
+#endif // LLVM_CLANG_SEMA_LOOPHINT_H
diff --git a/clang/lib/AST/StmtPrinter.cpp b/clang/lib/AST/StmtPrinter.cpp index 297de5e09ab..1daab32e5b7 100644 --- a/clang/lib/AST/StmtPrinter.cpp +++ b/clang/lib/AST/StmtPrinter.cpp @@ -168,8 +168,22 @@ void StmtPrinter::VisitLabelStmt(LabelStmt *Node) { } void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) { - for (const auto *Attr : Node->getAttrs()) - Attr->printPretty(OS, Policy); + std::string raw_attr_os; + llvm::raw_string_ostream AttrOS(raw_attr_os); + for (const auto *Attr : Node->getAttrs()) { + // FIXME: This hack will be removed when printPretty + // has been modified to print pretty pragmas + if (const LoopHintAttr *LHA = dyn_cast<LoopHintAttr>(Attr)) { + LHA->print(OS, Policy); + } else + Attr->printPretty(AttrOS, Policy); + } + + // Print attributes after pragmas. + StringRef AttrStr = AttrOS.str(); + if (!AttrStr.empty()) + OS << AttrStr; + PrintStmt(Node->getSubStmt(), 0); } diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp index 534c16deeea..cca16249000 100644 --- a/clang/lib/CodeGen/CGStmt.cpp +++ b/clang/lib/CodeGen/CGStmt.cpp @@ -18,6 +18,7 @@ #include "clang/AST/StmtVisitor.h" #include "clang/Basic/PrettyStackTrace.h" #include "clang/Basic/TargetInfo.h" +#include "clang/Sema/LoopHint.h" #include "clang/Sema/SemaDiagnostic.h" #include "llvm/ADT/StringExtras.h" #include "llvm/IR/CallSite.h" @@ -398,7 +399,23 @@ void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) { } void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) { - EmitStmt(S.getSubStmt()); + const Stmt *SubStmt = S.getSubStmt(); + switch (SubStmt->getStmtClass()) { + case Stmt::DoStmtClass: + EmitDoStmt(cast<DoStmt>(*SubStmt), S.getAttrs()); + break; + case Stmt::ForStmtClass: + EmitForStmt(cast<ForStmt>(*SubStmt), S.getAttrs()); + break; + case Stmt::WhileStmtClass: + EmitWhileStmt(cast<WhileStmt>(*SubStmt), S.getAttrs()); + break; + case Stmt::CXXForRangeStmtClass: + EmitCXXForRangeStmt(cast<CXXForRangeStmt>(*SubStmt), S.getAttrs()); + break; + default: + EmitStmt(SubStmt); + } } void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) { @@ -504,7 +521,78 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) { EmitBlock(ContBlock, true); } -void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) { +void CodeGenFunction::EmitCondBrHints(llvm::LLVMContext &Context, + llvm::BranchInst *CondBr, + const ArrayRef<const Attr *> &Attrs) { + // Return if there are no hints. + if (Attrs.empty()) + return; + + // Add vectorize hints to the metadata on the conditional branch. + SmallVector<llvm::Value *, 2> Metadata(1); + for (const auto *Attr : Attrs) { + const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(Attr); + + // Skip non loop hint attributes + if (!LH) + continue; + + LoopHintAttr::OptionType Option = LH->getOption(); + int ValueInt = LH->getValue(); + + const char *MetadataName; + switch (Option) { + case LoopHintAttr::Vectorize: + case LoopHintAttr::VectorizeWidth: + MetadataName = "llvm.vectorizer.width"; + break; + case LoopHintAttr::Interleave: + case LoopHintAttr::InterleaveCount: + MetadataName = "llvm.vectorizer.unroll"; + break; + } + + llvm::Value *Value; + llvm::MDString *Name; + switch (Option) { + case LoopHintAttr::Vectorize: + case LoopHintAttr::Interleave: + if (ValueInt == 1) { + // FIXME: In the future I will modifiy the behavior of the metadata + // so we can enable/disable vectorization and interleaving separately. + Name = llvm::MDString::get(Context, "llvm.vectorizer.enable"); + Value = Builder.getTrue(); + break; + } + // Vectorization/interleaving is disabled, set width/count to 1. + ValueInt = 1; + // Fallthrough. + case LoopHintAttr::VectorizeWidth: + case LoopHintAttr::InterleaveCount: + Name = llvm::MDString::get(Context, MetadataName); + Value = llvm::ConstantInt::get(Int32Ty, ValueInt); + break; + } + + SmallVector<llvm::Value *, 2> OpValues; + OpValues.push_back(Name); + OpValues.push_back(Value); + + // Set or overwrite metadata indicated by Name. + Metadata.push_back(llvm::MDNode::get(Context, OpValues)); + } + + if (!Metadata.empty()) { + // Add llvm.loop MDNode to CondBr. + llvm::MDNode *LoopID = llvm::MDNode::get(Context, Metadata); + LoopID->replaceOperandWith(0, LoopID); // First op points to itself. + + CondBr->setMetadata("llvm.loop", LoopID); + } +} + +void CodeGenFunction::EmitWhileStmt(const WhileStmt &S, + const ArrayRef<const Attr *> &WhileAttrs) { RegionCounter Cnt = getPGORegionCounter(&S); // Emit the header for the loop, which will also become @@ -551,13 +639,17 @@ void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) { llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); if (ConditionScope.requiresCleanups()) ExitBlock = createBasicBlock("while.exit"); - Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock, - PGO.createLoopWeights(S.getCond(), Cnt)); + llvm::BranchInst *CondBr = + Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock, + PGO.createLoopWeights(S.getCond(), Cnt)); if (ExitBlock != LoopExit.getBlock()) { EmitBlock(ExitBlock); EmitBranchThroughCleanup(LoopExit); } + + // Attach metadata to loop body conditional branch. + EmitCondBrHints(LoopBody->getContext(), CondBr, WhileAttrs); } // Emit the loop body. We have to emit this in a cleanup scope @@ -588,7 +680,8 @@ void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) { SimplifyForwardingBlocks(LoopHeader.getBlock()); } -void CodeGenFunction::EmitDoStmt(const DoStmt &S) { +void CodeGenFunction::EmitDoStmt(const DoStmt &S, + const ArrayRef<const Attr *> &DoAttrs) { JumpDest LoopExit = getJumpDestInCurrentScope("do.end"); JumpDest LoopCond = getJumpDestInCurrentScope("do.cond"); @@ -628,9 +721,14 @@ void CodeGenFunction::EmitDoStmt(const DoStmt &S) { EmitBoolCondBranch = false; // As long as the condition is true, iterate the loop. - if (EmitBoolCondBranch) - Builder.CreateCondBr(BoolCondVal, LoopBody, LoopExit.getBlock(), - PGO.createLoopWeights(S.getCond(), Cnt)); + if (EmitBoolCondBranch) { + llvm::BranchInst *CondBr = + Builder.CreateCondBr(BoolCondVal, LoopBody, LoopExit.getBlock(), + PGO.createLoopWeights(S.getCond(), Cnt)); + + // Attach metadata to loop body conditional branch. + EmitCondBrHints(LoopBody->getContext(), CondBr, DoAttrs); + } LoopStack.pop(); @@ -643,7 +741,8 @@ void CodeGenFunction::EmitDoStmt(const DoStmt &S) { SimplifyForwardingBlocks(LoopCond.getBlock()); } -void CodeGenFunction::EmitForStmt(const ForStmt &S) { +void CodeGenFunction::EmitForStmt(const ForStmt &S, + const ArrayRef<const Attr *> &ForAttrs) { JumpDest LoopExit = getJumpDestInCurrentScope("for.end"); RunCleanupsScope ForScope(*this); @@ -699,8 +798,12 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S) { // C99 6.8.5p2/p4: The first substatement is executed if the expression // compares unequal to 0. The condition must be a scalar type. llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); - Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, - PGO.createLoopWeights(S.getCond(), Cnt)); + llvm::BranchInst *CondBr = + Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, + PGO.createLoopWeights(S.getCond(), Cnt)); + + // Attach metadata to loop body conditional branch. + EmitCondBrHints(ForBody->getContext(), CondBr, ForAttrs); if (ExitBlock != LoopExit.getBlock()) { EmitBlock(ExitBlock); @@ -743,7 +846,9 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S) { EmitBlock(LoopExit.getBlock(), true); } -void CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S) { +void +CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S, + const ArrayRef<const Attr *> &ForAttrs) { JumpDest LoopExit = getJumpDestInCurrentScope("for.end"); RunCleanupsScope ForScope(*this); @@ -778,8 +883,11 @@ void CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S) { // The body is executed if the expression, contextually converted // to bool, is true. llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); - Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, - PGO.createLoopWeights(S.getCond(), Cnt)); + llvm::BranchInst *CondBr = Builder.CreateCondBr( + BoolCondVal, ForBody, ExitBlock, PGO.createLoopWeights(S.getCond(), Cnt)); + + // Attach metadata to loop body conditional branch. + EmitCondBrHints(ForBody->getContext(), CondBr, ForAttrs); if (ExitBlock != LoopExit.getBlock()) { EmitBlock(ExitBlock); diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h index b6c58e0ee16..570e1a89eb0 100644 --- a/clang/lib/CodeGen/CodeGenFunction.h +++ b/clang/lib/CodeGen/CodeGenFunction.h @@ -1860,9 +1860,14 @@ public: void EmitGotoStmt(const GotoStmt &S); void EmitIndirectGotoStmt(const IndirectGotoStmt &S); void EmitIfStmt(const IfStmt &S); - void EmitWhileStmt(const WhileStmt &S); - void EmitDoStmt(const DoStmt &S); - void EmitForStmt(const ForStmt &S); + + void EmitCondBrHints(llvm::LLVMContext &Context, llvm::BranchInst *CondBr, + const ArrayRef<const Attr *> &Attrs); + void EmitWhileStmt(const WhileStmt &S, + const ArrayRef<const Attr *> &Attrs = None); + void EmitDoStmt(const DoStmt &S, const ArrayRef<const Attr *> &Attrs = None); + void EmitForStmt(const ForStmt &S, + const ArrayRef<const Attr *> &Attrs = None); void EmitReturnStmt(const ReturnStmt &S); void EmitDeclStmt(const DeclStmt &S); void EmitBreakStmt(const BreakStmt &S); @@ -1886,7 +1891,8 @@ public: void EmitCXXTryStmt(const CXXTryStmt &S); void EmitSEHTryStmt(const SEHTryStmt &S); - void EmitCXXForRangeStmt(const CXXForRangeStmt &S); + void EmitCXXForRangeStmt(const CXXForRangeStmt &S, + const ArrayRef<const Attr *> &Attrs = None); llvm::Function *EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K); llvm::Function *GenerateCapturedStmtFunction(const CapturedDecl *CD, diff --git a/clang/lib/Parse/ParsePragma.cpp b/clang/lib/Parse/ParsePragma.cpp index 787d3f0bdf4..427a44d8901 100644 --- a/clang/lib/Parse/ParsePragma.cpp +++ b/clang/lib/Parse/ParsePragma.cpp @@ -12,12 +12,13 @@ //===----------------------------------------------------------------------===// #include "RAIIObjectsForParser.h" -#include "clang/Lex/Preprocessor.h" -#include "clang/Parse/ParseDiagnostic.h" -#include "clang/Parse/Parser.h" -#include "clang/Sema/Scope.h" -#include "llvm/ADT/StringSwitch.h" -using namespace clang; +#include "clang/Lex/Preprocessor.h"
+#include "clang/Parse/ParseDiagnostic.h"
+#include "clang/Parse/Parser.h"
+#include "clang/Sema/LoopHint.h"
+#include "clang/Sema/Scope.h"
+#include "llvm/ADT/StringSwitch.h"
+using namespace clang;
namespace { @@ -138,12 +139,18 @@ struct PragmaOptimizeHandler : public PragmaHandler { void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, Token &FirstToken) override; private: - Sema &Actions; -}; - -} // end namespace - -void Parser::initializePragmaHandlers() { + Sema &Actions;
+};
+
+struct PragmaLoopHintHandler : public PragmaHandler {
+ PragmaLoopHintHandler() : PragmaHandler("loop") {}
+ void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
+ Token &FirstToken) override;
+};
+
+} // end namespace
+
+void Parser::initializePragmaHandlers() {
AlignHandler.reset(new PragmaAlignHandler()); PP.AddPragmaHandler(AlignHandler.get()); @@ -205,12 +212,15 @@ void Parser::initializePragmaHandlers() { MSSection.reset(new PragmaMSPragma("section")); PP.AddPragmaHandler(MSSection.get()); } - - OptimizeHandler.reset(new PragmaOptimizeHandler(Actions)); - PP.AddPragmaHandler("clang", OptimizeHandler.get()); -} - -void Parser::resetPragmaHandlers() { +
+ OptimizeHandler.reset(new PragmaOptimizeHandler(Actions));
+ PP.AddPragmaHandler("clang", OptimizeHandler.get());
+
+ LoopHintHandler.reset(new PragmaLoopHintHandler());
+ PP.AddPragmaHandler("clang", LoopHintHandler.get());
+}
+
+void Parser::resetPragmaHandlers() {
// Remove the pragma handlers we installed. PP.RemovePragmaHandler(AlignHandler.get()); AlignHandler.reset(); @@ -262,12 +272,15 @@ void Parser::resetPragmaHandlers() { PP.RemovePragmaHandler("STDC", FPContractHandler.get()); FPContractHandler.reset(); - - PP.RemovePragmaHandler("clang", OptimizeHandler.get()); - OptimizeHandler.reset(); -} - -/// \brief Handle the annotation token produced for #pragma unused(...) +
+ PP.RemovePragmaHandler("clang", OptimizeHandler.get());
+ OptimizeHandler.reset();
+
+ PP.RemovePragmaHandler("clang", LoopHintHandler.get());
+ LoopHintHandler.reset();
+}
+
+/// \brief Handle the annotation token produced for #pragma unused(...)
/// /// Each annot_pragma_unused is followed by the argument token so e.g. /// "#pragma unused(x,y)" becomes: @@ -583,12 +596,46 @@ unsigned Parser::HandlePragmaMSSegment(llvm::StringRef PragmaName, unsigned Parser::HandlePragmaMSInitSeg(llvm::StringRef PragmaName, SourceLocation PragmaLocation) { return PP.getDiagnostics().getCustomDiagID( - DiagnosticsEngine::Error, "'#pragma %0' not implemented."); -} - -// #pragma GCC visibility comes in two variants: -// 'push' '(' [visibility] ')' -// 'pop' + DiagnosticsEngine::Error, "'#pragma %0' not implemented.");
+}
+
+struct PragmaLoopHintInfo {
+ Token Loop;
+ Token Value;
+ Token Option;
+};
+
+LoopHint Parser::HandlePragmaLoopHint() {
+ assert(Tok.is(tok::annot_pragma_loop_hint));
+ PragmaLoopHintInfo *Info =
+ static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
+
+ LoopHint Hint;
+ Hint.LoopLoc =
+ IdentifierLoc::create(Actions.Context, Info->Loop.getLocation(),
+ Info->Loop.getIdentifierInfo());
+ Hint.OptionLoc =
+ IdentifierLoc::create(Actions.Context, Info->Option.getLocation(),
+ Info->Option.getIdentifierInfo());
+ Hint.ValueLoc =
+ IdentifierLoc::create(Actions.Context, Info->Value.getLocation(),
+ Info->Value.getIdentifierInfo());
+ Hint.Range =
+ SourceRange(Info->Option.getLocation(), Info->Value.getLocation());
+
+ // FIXME: We should support template parameters for the loop hint value.
+ // See bug report #19610
+ if (Info->Value.is(tok::numeric_constant))
+ Hint.ValueExpr = Actions.ActOnNumericConstant(Info->Value).get();
+ else
+ Hint.ValueExpr = nullptr;
+
+ return Hint;
+}
+
+// #pragma GCC visibility comes in two variants:
+// 'push' '(' [visibility] ')'
+// 'pop'
void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, Token &VisTok) { @@ -1581,6 +1628,113 @@ void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP, << PP.getSpelling(Tok); return; } - - Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation()); -} +
+ Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
+}
+
+/// \brief Handle the \#pragma clang loop directive.
+/// #pragma clang 'loop' loop-hints
+///
+/// loop-hints:
+/// loop-hint loop-hints[opt]
+///
+/// loop-hint:
+/// 'vectorize' '(' loop-hint-keyword ')'
+/// 'interleave' '(' loop-hint-keyword ')'
+/// 'vectorize_width' '(' loop-hint-value ')'
+/// 'interleave_count' '(' loop-hint-value ')'
+///
+/// loop-hint-keyword:
+/// 'enable'
+/// 'disable'
+///
+/// loop-hint-value:
+/// constant-expression
+///
+/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
+/// try vectorizing the instructions of the loop it precedes. Specifying
+/// interleave(enable) or interleave_count(_value_) instructs llvm to try
+/// interleaving multiple iterations of the loop it precedes. The width of the
+/// vector instructions is specified by vectorize_width() and the number of
+/// interleaved loop iterations is specified by interleave_count(). Specifying a
+/// value of 1 effectively disables vectorization/interleaving, even if it is
+/// possible and profitable, and 0 is invalid. The loop vectorizer currently
+/// only works on inner loops.
+///
+void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
+ PragmaIntroducerKind Introducer,
+ Token &Tok) {
+ Token Loop = Tok;
+ SmallVector<Token, 1> TokenList;
+
+ // Lex the optimization option and verify it is an identifier.
+ PP.Lex(Tok);
+ if (Tok.isNot(tok::identifier)) {
+ PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
+ << /*MissingOption=*/true << "";
+ return;
+ }
+
+ while (Tok.is(tok::identifier)) {
+ Token Option = Tok;
+ IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
+
+ if (!OptionInfo->isStr("vectorize") && !OptionInfo->isStr("interleave") &&
+ !OptionInfo->isStr("vectorize_width") &&
+ !OptionInfo->isStr("interleave_count")) {
+ PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
+ << /*MissingOption=*/false << OptionInfo;
+ return;
+ }
+
+ // Read '('
+ PP.Lex(Tok);
+ if (Tok.isNot(tok::l_paren)) {
+ PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
+ return;
+ }
+
+ // FIXME: All tokens between '(' and ')' should be stored and parsed as a
+ // constant expression.
+ PP.Lex(Tok);
+ Token Value;
+ if (Tok.is(tok::identifier) || Tok.is(tok::numeric_constant))
+ Value = Tok;
+
+ // Read ')'
+ PP.Lex(Tok);
+ if (Tok.isNot(tok::r_paren)) {
+ PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
+ return;
+ }
+
+ // Get next optimization option.
+ PP.Lex(Tok);
+
+ auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
+ Info->Loop = Loop;
+ Info->Option = Option;
+ Info->Value = Value;
+
+ // Generate the vectorization hint token.
+ Token LoopHintTok;
+ LoopHintTok.startToken();
+ LoopHintTok.setKind(tok::annot_pragma_loop_hint);
+ LoopHintTok.setLocation(Loop.getLocation());
+ LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
+ TokenList.push_back(LoopHintTok);
+ }
+
+ if (Tok.isNot(tok::eod)) {
+ PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
+ << "clang loop";
+ return;
+ }
+
+ Token *TokenArray = new Token[TokenList.size()];
+ std::copy(TokenList.begin(), TokenList.end(), TokenArray);
+
+ PP.EnterTokenStream(TokenArray, TokenList.size(),
+ /*DisableMacroExpansion=*/false,
+ /*OwnsTokens=*/true);
+}
diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp index 1ee7c965c6f..210963ec0c9 100644 --- a/clang/lib/Parse/ParseStmt.cpp +++ b/clang/lib/Parse/ParseStmt.cpp @@ -15,11 +15,13 @@ #include "clang/Parse/Parser.h" #include "RAIIObjectsForParser.h" #include "clang/AST/ASTContext.h" +#include "clang/Basic/Attributes.h" #include "clang/Basic/Diagnostic.h" #include "clang/Basic/PrettyStackTrace.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/TargetInfo.h" #include "clang/Sema/DeclSpec.h" +#include "clang/Sema/LoopHint.h" #include "clang/Sema/PrettyDeclStackTrace.h" #include "clang/Sema/Scope.h" #include "clang/Sema/TypoCorrection.h" @@ -357,6 +359,10 @@ Retry: ProhibitAttributes(Attrs); HandlePragmaMSPragma(); return StmtEmpty(); + + case tok::annot_pragma_loop_hint: + ProhibitAttributes(Attrs); + return ParsePragmaLoopHint(Stmts, OnlyStatement, TrailingElseLoc, Attrs); } // If we reached this code, the statement must end in a semicolon. @@ -1759,6 +1765,37 @@ StmtResult Parser::ParseReturnStatement() { return Actions.ActOnReturnStmt(ReturnLoc, R.get(), getCurScope()); } +StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts, bool OnlyStatement, + SourceLocation *TrailingElseLoc, + ParsedAttributesWithRange &Attrs) { + // Create temporary attribute list. + ParsedAttributesWithRange TempAttrs(AttrFactory); + + // Get vectorize hints and consume annotated token. + while (Tok.is(tok::annot_pragma_loop_hint)) { + LoopHint Hint = HandlePragmaLoopHint(); + ConsumeToken(); + + if (!Hint.LoopLoc || !Hint.OptionLoc || !Hint.ValueLoc) + continue; + + ArgsUnion ArgHints[] = {Hint.OptionLoc, Hint.ValueLoc, + ArgsUnion(Hint.ValueExpr)}; + // FIXME: Replace AS_Keyword with Pragma spelling AS_Pragma. + TempAttrs.addNew(Hint.LoopLoc->Ident, Hint.Range, 0, Hint.LoopLoc->Loc, + ArgHints, 3, AttributeList::AS_Keyword); + } + + // Get the next statement. + MaybeParseCXX11Attributes(Attrs); + + StmtResult S = ParseStatementOrDeclarationAfterAttributes( + Stmts, OnlyStatement, TrailingElseLoc, Attrs); + + Attrs.takeAllFrom(TempAttrs); + return S; +} + namespace { class ClangAsmParserCallback : public llvm::MCAsmParserSemaCallback { Parser &TheParser; diff --git a/clang/lib/Sema/SemaStmtAttr.cpp b/clang/lib/Sema/SemaStmtAttr.cpp index 3bc620b89ba..4e3999bf737 100644 --- a/clang/lib/Sema/SemaStmtAttr.cpp +++ b/clang/lib/Sema/SemaStmtAttr.cpp @@ -13,12 +13,13 @@ #include "clang/Sema/SemaInternal.h" #include "clang/AST/ASTContext.h" -#include "clang/Basic/SourceManager.h" -#include "clang/Sema/DelayedDiagnostic.h" -#include "clang/Sema/Lookup.h" -#include "clang/Sema/ScopeInfo.h" -#include "llvm/ADT/StringExtras.h" - +#include "clang/Basic/SourceManager.h"
+#include "clang/Sema/DelayedDiagnostic.h"
+#include "clang/Sema/Lookup.h"
+#include "clang/Sema/LoopHint.h"
+#include "clang/Sema/ScopeInfo.h"
+#include "llvm/ADT/StringExtras.h"
+
using namespace clang; using namespace sema; @@ -39,23 +40,175 @@ static Attr *handleFallThroughAttr(Sema &S, Stmt *St, const AttributeList &A, return nullptr; } return ::new (S.Context) FallThroughAttr(A.getRange(), S.Context, - A.getAttributeSpellingListIndex()); -} - - -static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A, - SourceRange Range) { - switch (A.getKind()) { + A.getAttributeSpellingListIndex());
+}
+
+static Attr *handleLoopHintAttr(Sema &S, Stmt *St, const AttributeList &A,
+ SourceRange) {
+ if (St->getStmtClass() != Stmt::DoStmtClass &&
+ St->getStmtClass() != Stmt::ForStmtClass &&
+ St->getStmtClass() != Stmt::CXXForRangeStmtClass &&
+ St->getStmtClass() != Stmt::WhileStmtClass) {
+ S.Diag(St->getLocStart(), diag::err_pragma_loop_precedes_nonloop);
+ return nullptr;
+ }
+
+ IdentifierLoc *OptionLoc = A.getArgAsIdent(0);
+ IdentifierInfo *OptionInfo = OptionLoc->Ident;
+ IdentifierLoc *ValueLoc = A.getArgAsIdent(1);
+ IdentifierInfo *ValueInfo = ValueLoc->Ident;
+ Expr *ValueExpr = A.getArgAsExpr(2);
+
+ assert(OptionInfo && "Attribute must have valid option info.");
+
+ LoopHintAttr::OptionType Option =
+ llvm::StringSwitch<LoopHintAttr::OptionType>(OptionInfo->getNameStart())
+ .Case("vectorize", LoopHintAttr::Vectorize)
+ .Case("vectorize_width", LoopHintAttr::VectorizeWidth)
+ .Case("interleave", LoopHintAttr::Interleave)
+ .Case("interleave_count", LoopHintAttr::InterleaveCount)
+ .Default(LoopHintAttr::Vectorize);
+
+ int ValueInt;
+ if (Option == LoopHintAttr::Vectorize || Option == LoopHintAttr::Interleave) {
+ if (!ValueInfo) {
+ S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_keyword)
+ << /*MissingKeyword=*/true << "";
+ return nullptr;
+ }
+
+ if (ValueInfo->isStr("disable"))
+ ValueInt = 0;
+ else if (ValueInfo->isStr("enable"))
+ ValueInt = 1;
+ else {
+ S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_keyword)
+ << /*MissingKeyword=*/false << ValueInfo;
+ return nullptr;
+ }
+ } else if (Option == LoopHintAttr::VectorizeWidth ||
+ Option == LoopHintAttr::InterleaveCount) {
+ // FIXME: We should support template parameters for the loop hint value.
+ // See bug report #19610.
+ llvm::APSInt ValueAPS;
+ if (!ValueExpr || !ValueExpr->isIntegerConstantExpr(ValueAPS, S.Context)) {
+ S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_value)
+ << /*MissingValue=*/true << "";
+ return nullptr;
+ }
+
+ if ((ValueInt = ValueAPS.getSExtValue()) < 1) {
+ S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_value)
+ << /*MissingValue=*/false << ValueInt;
+ return nullptr;
+ }
+ }
+
+ return LoopHintAttr::CreateImplicit(S.Context, Option, ValueInt,
+ A.getRange());
+}
+
+static void
+CheckForIncompatibleAttributes(Sema &S, SmallVectorImpl<const Attr *> &Attrs) {
+ int PrevOptionValue[4] = {-1, -1, -1, -1};
+ int OptionId[4] = {LoopHintAttr::Vectorize, LoopHintAttr::VectorizeWidth,
+ LoopHintAttr::Interleave, LoopHintAttr::InterleaveCount};
+
+ for (const auto *I : Attrs) {
+ const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
+
+ // Skip non loop hint attributes
+ if (!LH)
+ continue;
+
+ int State, Value;
+ int Option = LH->getOption();
+ int ValueInt = LH->getValue();
+
+ switch (Option) {
+ case LoopHintAttr::Vectorize:
+ case LoopHintAttr::VectorizeWidth:
+ State = 0;
+ Value = 1;
+ break;
+ case LoopHintAttr::Interleave:
+ case LoopHintAttr::InterleaveCount:
+ State = 2;
+ Value = 3;
+ break;
+ }
+
+ SourceLocation ValueLoc = LH->getRange().getEnd();
+
+ // Compatibility testing is split into two cases.
+ // 1. if the current loop hint sets state (enable/disable) - check against
+ // previous state and value.
+ // 2. if the current loop hint sets a value - check against previous state
+ // and value.
+
+ if (Option == State) {
+ if (PrevOptionValue[State] != -1) {
+ // Cannot specify state twice.
+ int PrevValue = PrevOptionValue[State];
+ S.Diag(ValueLoc, diag::err_pragma_loop_compatibility)
+ << /*Duplicate=*/true << LoopHintAttr::getOptionName(Option)
+ << LoopHintAttr::getValueName(PrevValue)
+ << LoopHintAttr::getOptionName(Option)
+ << LoopHintAttr::getValueName(Value);
+ }
+
+ if (PrevOptionValue[Value] != -1) {
+ // Compare state with previous width/count.
+ int PrevOption = OptionId[Value];
+ int PrevValueInt = PrevOptionValue[Value];
+ if ((ValueInt == 0 && PrevValueInt > 1) ||
+ (ValueInt == 1 && PrevValueInt <= 1))
+ S.Diag(ValueLoc, diag::err_pragma_loop_compatibility)
+ << /*Duplicate=*/false << LoopHintAttr::getOptionName(PrevOption)
+ << PrevValueInt << LoopHintAttr::getOptionName(Option)
+ << LoopHintAttr::getValueName(ValueInt);
+ }
+ } else {
+ if (PrevOptionValue[State] != -1) {
+ // Compare width/count value with previous state.
+ int PrevOption = OptionId[State];
+ int PrevValueInt = PrevOptionValue[State];
+ if ((ValueInt > 1 && PrevValueInt == 0) ||
+ (ValueInt <= 1 && PrevValueInt == 1))
+ S.Diag(ValueLoc, diag::err_pragma_loop_compatibility)
+ << /*Duplicate=*/false << LoopHintAttr::getOptionName(PrevOption)
+ << LoopHintAttr::getValueName(PrevValueInt)
+ << LoopHintAttr::getOptionName(Option) << ValueInt;
+ }
+
+ if (PrevOptionValue[Value] != -1) {
+ // Cannot specify a width/count twice.
+ int PrevValueInt = PrevOptionValue[Value];
+ S.Diag(ValueLoc, diag::err_pragma_loop_compatibility)
+ << /*Duplicate=*/true << LoopHintAttr::getOptionName(Option)
+ << PrevValueInt << LoopHintAttr::getOptionName(Option) << ValueInt;
+ }
+ }
+
+ PrevOptionValue[Option] = ValueInt;
+ }
+}
+
+static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A,
+ SourceRange Range) {
+ switch (A.getKind()) {
case AttributeList::UnknownAttribute: S.Diag(A.getLoc(), A.isDeclspecAttribute() ? diag::warn_unhandled_ms_attribute_ignored : diag::warn_unknown_attribute_ignored) << A.getName(); - return nullptr; - case AttributeList::AT_FallThrough: - return handleFallThroughAttr(S, St, A, Range); - default: - // if we're here, then we parsed a known attribute, but didn't recognize - // it as a statement attribute => it is declaration attribute + return nullptr;
+ case AttributeList::AT_FallThrough:
+ return handleFallThroughAttr(S, St, A, Range);
+ case AttributeList::AT_LoopHint:
+ return handleLoopHintAttr(S, St, A, Range);
+ default:
+ // if we're here, then we parsed a known attribute, but didn't recognize
+ // it as a statement attribute => it is declaration attribute
S.Diag(A.getRange().getBegin(), diag::err_attribute_invalid_on_stmt) << A.getName() << St->getLocStart(); return nullptr; @@ -67,11 +220,13 @@ StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList, SmallVector<const Attr*, 8> Attrs; for (const AttributeList* l = AttrList; l; l = l->getNext()) { if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range)) - Attrs.push_back(a); - } - - if (Attrs.empty()) - return S; - + Attrs.push_back(a);
+ }
+
+ CheckForIncompatibleAttributes(*this, Attrs);
+
+ if (Attrs.empty())
+ return S;
+
return ActOnAttributedStmt(Range.getBegin(), Attrs, S); } diff --git a/clang/test/CodeGen/pragma-loop.cpp b/clang/test/CodeGen/pragma-loop.cpp new file mode 100644 index 00000000000..b00b0c04305 --- /dev/null +++ b/clang/test/CodeGen/pragma-loop.cpp @@ -0,0 +1,120 @@ +// RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++11 -emit-llvm -o - %s | FileCheck %s
+
+// Verify while loop is recognized after sequence of pragma clang loop directives.
+void while_test(int *List, int Length) {
+ // CHECK: define {{.*}} @_Z10while_test
+ int i = 0;
+
+#pragma clang loop vectorize(enable)
+#pragma clang loop interleave_count(4)
+#pragma clang loop vectorize_width(4)
+ while (i < Length) {
+ // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_1:.*]]
+ List[i] = i * 2;
+ i++;
+ }
+}
+
+// Verify do loop is recognized after multi-option pragma clang loop directive.
+void do_test(int *List, int Length) {
+ int i = 0;
+
+#pragma clang loop vectorize_width(8) interleave_count(4)
+ do {
+ // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_2:.*]]
+ List[i] = i * 2;
+ i++;
+ } while (i < Length);
+}
+
+// Verify for loop is recognized after sequence of pragma clang loop directives.
+void for_test(int *List, int Length) {
+#pragma clang loop interleave(enable)
+#pragma clang loop interleave_count(4)
+ for (int i = 0; i < Length; i++) {
+ // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_3:.*]]
+ List[i] = i * 2;
+ }
+}
+
+// Verify c++11 for range loop is recognized after
+// sequence of pragma clang loop directives.
+void for_range_test() {
+ double List[100];
+
+#pragma clang loop vectorize_width(2) interleave_count(2)
+ for (int i : List) {
+ // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_4:.*]]
+ List[i] = i;
+ }
+}
+
+// Verify disable pragma clang loop directive generates correct metadata
+void disable_test(int *List, int Length) {
+#pragma clang loop vectorize(disable)
+ for (int i = 0; i < Length; i++) {
+ // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_5:.*]]
+ List[i] = i * 2;
+ }
+}
+
+#define VECWIDTH 2
+#define INTCOUNT 2
+
+// Verify defines are correctly resolved in pragma clang loop directive
+void for_define_test(int *List, int Length, int Value) {
+#pragma clang loop vectorize_width(VECWIDTH) interleave_count(INTCOUNT)
+ for (int i = 0; i < Length; i++) {
+ // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_6:.*]]
+ List[i] = i * Value;
+ }
+}
+
+// Verify metadata is generated when template is used.
+template <typename A>
+void for_template_test(A *List, int Length, A Value) {
+
+#pragma clang loop vectorize_width(8) interleave_count(8)
+ for (int i = 0; i < Length; i++) {
+ // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_7:.*]]
+ List[i] = i * Value;
+ }
+}
+
+// Verify define is resolved correctly when template is used.
+template <typename A>
+void for_template_define_test(A *List, int Length, A Value) {
+#pragma clang loop vectorize_width(VECWIDTH) interleave_count(INTCOUNT)
+ for (int i = 0; i < Length; i++) {
+ // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_8:.*]]
+ List[i] = i * Value;
+ }
+}
+
+#undef VECWIDTH
+#undef INTCOUNT
+
+// Use templates defined above. Test verifies metadata is generated correctly.
+void template_test(double *List, int Length) {
+ double Value = 10;
+
+ for_template_test<double>(List, Length, Value);
+ for_template_define_test<double>(List, Length, Value);
+}
+
+// CHECK: ![[LOOP_1]] = metadata !{metadata ![[LOOP_1]], metadata ![[WIDTH_4:.*]], metadata ![[UNROLL_4:.*]], metadata ![[ENABLE_1:.*]]}
+// CHECK: ![[WIDTH_4]] = metadata !{metadata !"llvm.vectorizer.width", i32 4}
+// CHECK: ![[UNROLL_4]] = metadata !{metadata !"llvm.vectorizer.unroll", i32 4}
+// CHECK: ![[ENABLE_1]] = metadata !{metadata !"llvm.vectorizer.enable", i1 true}
+// CHECK: ![[LOOP_2]] = metadata !{metadata ![[LOOP_2:.*]], metadata ![[UNROLL_4:.*]], metadata ![[WIDTH_8:.*]]}
+// CHECK: ![[WIDTH_8]] = metadata !{metadata !"llvm.vectorizer.width", i32 8}
+// CHECK: ![[LOOP_3]] = metadata !{metadata ![[LOOP_3]], metadata ![[UNROLL_4:.*]], metadata ![[ENABLE_1:.*]]}
+// CHECK: ![[LOOP_4]] = metadata !{metadata ![[LOOP_4]], metadata ![[UNROLL_2:.*]], metadata ![[WIDTH_2:.*]]}
+// CHECK: ![[UNROLL_2]] = metadata !{metadata !"llvm.vectorizer.unroll", i32 2}
+// CHECK: ![[WIDTH_2]] = metadata !{metadata !"llvm.vectorizer.width", i32 2}
+// CHECK: ![[LOOP_5]] = metadata !{metadata ![[LOOP_5]], metadata ![[WIDTH_1:.*]]}
+// CHECK: ![[WIDTH_1]] = metadata !{metadata !"llvm.vectorizer.width", i32 1}
+// CHECK: ![[LOOP_6]] = metadata !{metadata ![[LOOP_6]], metadata ![[UNROLL_2:.*]], metadata ![[WIDTH_2:.*]]}
+// CHECK: ![[LOOP_7]] = metadata !{metadata ![[LOOP_7]], metadata ![[UNROLL_8:.*]], metadata ![[WIDTH_8:.*]]}
+// CHECK: ![[UNROLL_8]] = metadata !{metadata !"llvm.vectorizer.unroll", i32 8}
+// CHECK: ![[LOOP_8]] = metadata !{metadata ![[LOOP_8]], metadata ![[UNROLL_2:.*]], metadata ![[WIDTH_2:.*]]}
diff --git a/clang/test/PCH/pragma-loop.cpp b/clang/test/PCH/pragma-loop.cpp new file mode 100644 index 00000000000..41bbf72db3d --- /dev/null +++ b/clang/test/PCH/pragma-loop.cpp @@ -0,0 +1,62 @@ +// RUN: %clang_cc1 -emit-pch -o %t.a %s
+// RUN: %clang_cc1 -include-pch %t.a %s -ast-print -o - | FileCheck %s
+
+// FIXME: A bug in ParsedAttributes causes the order of the attributes to be
+// reversed. The checks are consequently in the reverse order below.
+
+// CHECK: #pragma clang loop interleave_count(8)
+// CHECK: #pragma clang loop vectorize_width(4)
+// CHECK: #pragma clang loop interleave(disable)
+// CHECK: #pragma clang loop vectorize(enable)
+// CHECK: #pragma clang loop interleave(enable)
+// CHECK: #pragma clang loop vectorize(disable)
+
+#ifndef HEADER
+#define HEADER
+
+class pragma_test {
+public:
+ inline void run1(int *List, int Length) {
+ int i = 0;
+#pragma clang loop vectorize_width(4)
+#pragma clang loop interleave_count(8)
+ while (i < Length) {
+ List[i] = i;
+ i++;
+ }
+ }
+
+ inline void run2(int *List, int Length) {
+ int i = 0;
+#pragma clang loop vectorize(enable)
+#pragma clang loop interleave(disable)
+ while (i - 1 < Length) {
+ List[i] = i;
+ i++;
+ }
+ }
+
+ inline void run3(int *List, int Length) {
+ int i = 0;
+#pragma clang loop vectorize(disable)
+#pragma clang loop interleave(enable)
+ while (i - 3 < Length) {
+ List[i] = i;
+ i++;
+ }
+ }
+};
+
+#else
+
+void test() {
+ int List[100];
+
+ pragma_test pt;
+
+ pt.run1(List, 100);
+ pt.run2(List, 100);
+ pt.run3(List, 100);
+}
+
+#endif
diff --git a/clang/test/Parser/pragma-loop.cpp b/clang/test/Parser/pragma-loop.cpp new file mode 100644 index 00000000000..e515eb462ad --- /dev/null +++ b/clang/test/Parser/pragma-loop.cpp @@ -0,0 +1,132 @@ +// RUN: %clang_cc1 -std=c++11 -verify %s
+
+// Note that this puts the expected lines before the directives to work around
+// limitations in the -verify mode.
+
+void test(int *List, int Length) {
+ int i = 0;
+
+#pragma clang loop vectorize(enable)
+#pragma clang loop interleave(enable)
+ while (i + 1 < Length) {
+ List[i] = i;
+ }
+
+#pragma clang loop vectorize_width(4)
+#pragma clang loop interleave_count(8)
+ while (i < Length) {
+ List[i] = i;
+ }
+
+#pragma clang loop vectorize(disable)
+#pragma clang loop interleave(disable)
+ while (i - 1 < Length) {
+ List[i] = i;
+ }
+
+#pragma clang loop vectorize_width(4) interleave_count(8)
+ while (i - 2 < Length) {
+ List[i] = i;
+ }
+
+#pragma clang loop interleave_count(16)
+ while (i - 3 < Length) {
+ List[i] = i;
+ }
+
+ int VList[Length];
+#pragma clang loop vectorize(disable) interleave(disable)
+ for (int j : VList) {
+ VList[j] = List[j];
+ }
+
+/* expected-error {{expected '('}} */ #pragma clang loop vectorize
+/* expected-error {{expected '('}} */ #pragma clang loop interleave
+
+/* expected-error {{expected ')'}} */ #pragma clang loop vectorize(enable
+/* expected-error {{expected ')'}} */ #pragma clang loop interleave(enable
+
+/* expected-error {{expected ')'}} */ #pragma clang loop vectorize_width(4
+/* expected-error {{expected ')'}} */ #pragma clang loop interleave_count(4
+
+/* expected-error {{missing option}} */ #pragma clang loop
+/* expected-error {{invalid option 'badkeyword'}} */ #pragma clang loop badkeyword
+/* expected-error {{invalid option 'badkeyword'}} */ #pragma clang loop badkeyword(enable)
+/* expected-error {{invalid option 'badkeyword'}} */ #pragma clang loop vectorize(enable) badkeyword(4)
+/* expected-warning {{extra tokens at end of '#pragma clang loop'}} */ #pragma clang loop vectorize(enable) ,
+
+ while (i-4 < Length) {
+ List[i] = i;
+ }
+
+/* expected-error {{invalid value 0; expected a positive integer value}} */ #pragma clang loop vectorize_width(0)
+/* expected-error {{invalid value 0; expected a positive integer value}} */ #pragma clang loop interleave_count(0)
+ while (i-5 < Length) {
+ List[i] = i;
+ }
+
+/* expected-error {{invalid value -1294967296; expected a positive integer value}} */ #pragma clang loop vectorize_width(3000000000)
+/* expected-error {{invalid value -1294967296; expected a positive integer value}} */ #pragma clang loop interleave_count(3000000000)
+ while (i-6 < Length) {
+ List[i] = i;
+ }
+
+/* expected-error {{missing value; expected a positive integer value}} */ #pragma clang loop vectorize_width(badvalue)
+/* expected-error {{missing value; expected a positive integer value}} */ #pragma clang loop interleave_count(badvalue)
+ while (i-6 < Length) {
+ List[i] = i;
+ }
+
+/* expected-error {{invalid keyword 'badidentifier'; expected 'enable' or 'disable'}} */ #pragma clang loop vectorize(badidentifier)
+/* expected-error {{invalid keyword 'badidentifier'; expected 'enable' or 'disable'}} */ #pragma clang loop interleave(badidentifier)
+ while (i-7 < Length) {
+ List[i] = i;
+ }
+
+#pragma clang loop vectorize(enable)
+/* expected-error {{expected a for, while, or do-while loop to follow the '#pragma clang loop' directive}} */ int j = Length;
+ List[0] = List[1];
+
+ while (j-1 < Length) {
+ List[j] = j;
+ }
+
+// FIXME: A bug in ParsedAttributes causes the order of the attributes to be
+// processed in reverse. Consequently, the errors occur on the first of pragma
+// of the next three tests rather than the last, and the order of the kinds
+// is also reversed.
+
+/* expected-error {{incompatible directives 'vectorize(disable)' and 'vectorize_width(4)'}} */ #pragma clang loop vectorize_width(4)
+#pragma clang loop vectorize(disable)
+/* expected-error {{incompatible directives 'interleave(disable)' and 'interleave_count(4)'}} */ #pragma clang loop interleave_count(4)
+#pragma clang loop interleave(disable)
+ while (i-8 < Length) {
+ List[i] = i;
+ }
+
+/* expected-error {{duplicate directives 'vectorize(disable)' and 'vectorize(enable)'}} */ #pragma clang loop vectorize(enable)
+#pragma clang loop vectorize(disable)
+/* expected-error {{duplicate directives 'interleave(disable)' and 'interleave(enable)'}} */ #pragma clang loop interleave(enable)
+#pragma clang loop interleave(disable)
+ while (i-9 < Length) {
+ List[i] = i;
+ }
+
+/* expected-error {{incompatible directives 'vectorize_width(4)' and 'vectorize(disable)'}} */ #pragma clang loop vectorize(disable)
+#pragma clang loop vectorize_width(4)
+/* expected-error {{incompatible directives 'interleave_count(4)' and 'interleave(disable)'}} */ #pragma clang loop interleave(disable)
+#pragma clang loop interleave_count(4)
+ while (i-10 < Length) {
+ List[i] = i;
+ }
+
+/* expected-error {{duplicate directives 'vectorize_width(4)' and 'vectorize_width(8)'}} */ #pragma clang loop vectorize_width(8)
+#pragma clang loop vectorize_width(4)
+/* expected-error {{duplicate directives 'interleave_count(4)' and 'interleave_count(8)'}} */ #pragma clang loop interleave_count(8)
+#pragma clang loop interleave_count(4)
+ while (i-11 < Length) {
+ List[i] = i;
+ }
+
+#pragma clang loop interleave(enable)
+/* expected-error {{expected statement}} */ }
|