diff --git a/include/clang/AST/Attr.h b/include/clang/AST/Attr.h
index fc48816..45dc150 100644
--- a/include/clang/AST/Attr.h
+++ b/include/clang/AST/Attr.h
@@ -111,0 +112,10 @@ public:
+  // Pretty print pragma attribute. Attributes with pragma spellings should
+  // overload this method.
+  // FIXME: TableGen should be modified to print attributes with pragma
+  // spellings in a generic way.
+  virtual void printPrettyPragma(raw_ostream &OS,
+                                 const PrintingPolicy &Policy) const {
+    llvm_unreachable("printPrettyPragma must be specified for attributes with "
+                     "a pragma spelling.");
+  }
+
diff --git a/include/clang/Basic/Attr.td b/include/clang/Basic/Attr.td
index ce8ca1a..7db476f 100644
--- a/include/clang/Basic/Attr.td
+++ b/include/clang/Basic/Attr.td
@@ -192,0 +193 @@ class Keyword<string name> : Spelling<name, "Keyword">;
+class Pragma<string name> : Spelling<name, "Pragma">;
@@ -1763,0 +1765,59 @@ def Unaligned : IgnoredAttr {
+
+def LoopHint : Attr {
+  // LoopHint Vectorize:
+  //  enable - use vector instructions.
+  //  disable - do not use vector instructions.
+  //  positive value - use vector instructions of the specified width.
+
+  // LoopHint Interleave:
+  //  enable - interleave multiple loop iterations.
+  //  disable - do not interleave multiple loop interactions.
+  //  positive value - interleave the specified number of loop interations.
+
+  let Spellings = [Pragma<"loop">];
+
+  // State of the loop optimization specified by the spelling.
+  let Args = [EnumArgument<"Option", "OptionType",
+                          ["vectorize", "interleave"],
+                          ["Vectorize", "Interleave"]>,
+              EnumArgument<"Arg", "ArgType",
+                          ["disable", "enable", "value"],
+                          ["Disable", "Enable", "Value"]>,
+              DefaultIntArgument<"Value", 1>];
+
+  let AdditionalMembers = [{
+  // Returns true if Args can co-exist a statement. Enable and Value are
+  // compatible. Disable is only compatible with itself.
+  static bool isCompatible(int Arg1, int Arg2) {
+    return (Arg1 == Disable) == (Arg2 == Disable);
+  }
+
+  static StringRef getOptionName(int Option) {
+    switch (Option) {
+    case Vectorize: return "vectorize";
+    case Interleave: return "interleave";
+    }
+    llvm_unreachable("Unhandled LoopHint option.");
+  }
+
+  static StringRef getArgName(int Arg) {
+    switch (Arg) {
+    case Disable: return "disable";
+    case Enable: return "enable";
+    case Value: return "value";
+    }
+    llvm_unreachable("Unhandled LoopHint arg.");
+  }
+
+  void printPrettyPragma(raw_ostream &OS, const PrintingPolicy &Policy) const {
+    OS << getOptionName(option) << "(";
+    if (getArg() == Value)
+      OS << value;
+    else
+      OS << getArgName(getArg());
+    OS << ")\n";
+  }
+  }];
+
+  let Documentation = [Undocumented];
+}
diff --git a/include/clang/Basic/Attributes.h b/include/clang/Basic/Attributes.h
index 4a7e462..5783b3b 100644
--- a/include/clang/Basic/Attributes.h
+++ b/include/clang/Basic/Attributes.h
@@ -28 +28,3 @@ enum class AttrSyntax {
-  CXX
+  CXX,
+  // Is the identifier known as a pragma attribute?
+  Pragma
diff --git a/include/clang/Basic/DiagnosticParseKinds.td b/include/clang/Basic/DiagnosticParseKinds.td
index 6a01dfc..428ecfa 100644
--- a/include/clang/Basic/DiagnosticParseKinds.td
+++ b/include/clang/Basic/DiagnosticParseKinds.td
@@ -894,0 +895,8 @@ def err_omp_more_one_clause : Error<
+
+// Pragma loop support.
+def err_pragma_loop_invalid_option : Error<
+  "%select{invalid|missing}0 option%select{ '%1'|}0 in directive "
+  "'#pragma loop'; expected either vectorize or interleave">;
+def err_pragma_loop_invalid_type : Error<
+  "invalid value '%0' in directive '#pragma loop %1'; expected either "
+  "'enable', 'disable', or a positive integer">;
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index 86e8b3e..9ca0df8 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -544,0 +545,7 @@ def note_surrounding_namespace_starts_here : Note<
+def err_pragma_loop_invalid_value : Error<
+  "expected a positive integer in directive '#pragma loop %0'">;
+def err_pragma_loop_incompatible : Error<
+  "'%0' and '%1' directive option types are incompatible in '#pragma loop %2'">;
+def err_pragma_loop_precedes_nonloop : Error<
+  "expected a for, while, or do-while loop to follow the '#pragma loop' "
+  "directive">;
diff --git a/include/clang/Basic/TokenKinds.def b/include/clang/Basic/TokenKinds.def
index 845a8b0..9c1d33b 100644
--- a/include/clang/Basic/TokenKinds.def
+++ b/include/clang/Basic/TokenKinds.def
@@ -703,0 +704,5 @@ ANNOTATION(pragma_openmp_end)
+// Annotations for loop pragma directives #pragma loop ...
+// The lexer produces these so that they only take effect when the parser
+// handles #pragma loop ... directives.
+ANNOTATION(pragma_loop_hint)
+
diff --git a/include/clang/Parse/Parser.h b/include/clang/Parse/Parser.h
index 83fa1a5..ecf5415 100644
--- a/include/clang/Parse/Parser.h
+++ b/include/clang/Parse/Parser.h
@@ -22,0 +23 @@
+#include "clang/Sema/LoopHint.h"
@@ -163,0 +165 @@ class Parser : public CodeCompletionHandler {
+  std::unique_ptr<PragmaHandler> LoopHintHandler;
@@ -521,0 +524,4 @@ private:
+  /// \brief Handle the annotation token produced for
+  /// #pragma vectorize...
+  LoopHint HandlePragmaLoopHint();
+
@@ -1603,0 +1610,3 @@ private:
+  StmtResult ParsePragmaLoopHint(StmtVector &Stmts, bool OnlyStatement,
+                                 SourceLocation *TrailingElseLoc,
+                                 ParsedAttributesWithRange &Attrs);
diff --git a/include/clang/Sema/AttributeList.h b/include/clang/Sema/AttributeList.h
index 6872ccc..24e4cd4 100644
--- a/include/clang/Sema/AttributeList.h
+++ b/include/clang/Sema/AttributeList.h
@@ -83 +83,3 @@ public:
-    AS_Keyword
+    AS_Keyword,
+    /// #pragma ...
+    AS_Pragma
diff --git a/include/clang/Sema/LoopHint.h b/include/clang/Sema/LoopHint.h
new file mode 100644
index 0000000..928cfb1
--- /dev/null
+++ b/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/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp
index 0804d40..6fc80ba 100644
--- a/lib/AST/StmtPrinter.cpp
+++ b/lib/AST/StmtPrinter.cpp
@@ -171 +171 @@ void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
-  for (const auto *Attr : Node->getAttrs())
+  for (const auto *Attr : Node->getAttrs()) {
@@ -172,0 +173,2 @@ void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
+  }
+
diff --git a/lib/CodeGen/CGStmt.cpp b/lib/CodeGen/CGStmt.cpp
index 573973a..48aeb3f 100644
--- a/lib/CodeGen/CGStmt.cpp
+++ b/lib/CodeGen/CGStmt.cpp
@@ -20,0 +21 @@
+#include "clang/Sema/LoopHint.h"
@@ -401 +402,17 @@ 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);
+  }
@@ -507 +524,51 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
-void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
+void CodeGenFunction::EmitCondBrHints(llvm::LLVMContext &Context,
+                                      llvm::BranchInst *CondBr,
+                                      ArrayRef<const Attr *> &Attrs) {
+  // Do not continue if there are not hints.
+  if (Attrs.empty())
+    return;
+
+  // Add vectorize hints to the metadata on the conditional branch.
+  // Iterate in reverse so hints are put in the same order they appear.
+  SmallVector<llvm::Value *, 2> Metadata(1);
+  for (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();
+    int Arg = LH->getArg();
+
+    llvm::Value *Value;
+    llvm::MDString *Name;
+    const char *MetadataNames[] = {"llvm.vectorizer.width",
+                                   "llvm.vectorizer.unroll"};
+    if (Arg == LoopHintAttr::Enable) {
+      Name = llvm::MDString::get(Context, "llvm.vectorizer.enable");
+      Value = Builder.getTrue();
+    } else {
+      // No need for the disable case because ValueInt is 1 if Arg is disable.
+      Name = llvm::MDString::get(Context, MetadataNames[Option]);
+      Value = llvm::ConstantInt::get(Int32Ty, ValueInt);
+    }
+
+    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));
+  }
+
+  // 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,
+                                    ArrayRef<const Attr *> WhileAttrs) {
@@ -554,2 +621,3 @@ void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
-    Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock,
-                         PGO.createLoopWeights(S.getCond(), Cnt));
+    llvm::BranchInst *CondBr =
+        Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock,
+                             PGO.createLoopWeights(S.getCond(), Cnt));
@@ -560,0 +629,3 @@ void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
+
+    // Attach metadata to loop body conditional branch.
+    EmitCondBrHints(LoopBody->getContext(), CondBr, WhileAttrs);
@@ -591 +662,2 @@ void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
-void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
+void CodeGenFunction::EmitDoStmt(const DoStmt &S,
+                                 ArrayRef<const Attr *> DoAttrs) {
@@ -631,3 +703,8 @@ void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
-  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);
+  }
@@ -646 +723,2 @@ void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
-void CodeGenFunction::EmitForStmt(const ForStmt &S) {
+void CodeGenFunction::EmitForStmt(const ForStmt &S,
+                                  ArrayRef<const Attr *> ForAttrs) {
@@ -702,2 +780,6 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S) {
-    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);
@@ -746 +828,2 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S) {
-void CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S) {
+void CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S,
+                                          ArrayRef<const Attr *> ForAttrs) {
@@ -781,2 +864,5 @@ void CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S) {
-  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);
diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h
index 750bec8..185a94d 100644
--- a/lib/CodeGen/CodeGenFunction.h
+++ b/lib/CodeGen/CodeGenFunction.h
@@ -1859,3 +1859,9 @@ public:
-  void EmitWhileStmt(const WhileStmt &S);
-  void EmitDoStmt(const DoStmt &S);
-  void EmitForStmt(const ForStmt &S);
+
+  void EmitCondBrHints(llvm::LLVMContext &Context, llvm::BranchInst *CondBr,
+                       ArrayRef<const Attr *> &Attrs);
+  void EmitWhileStmt(const WhileStmt &S,
+                     ArrayRef<const Attr *> Attrs = ArrayRef<const Attr *>());
+  void EmitDoStmt(const DoStmt &S,
+                  ArrayRef<const Attr *> Attrs = ArrayRef<const Attr *>());
+  void EmitForStmt(const ForStmt &S,
+                   ArrayRef<const Attr *> Attrs = ArrayRef<const Attr *>());
@@ -1885 +1891,3 @@ public:
-  void EmitCXXForRangeStmt(const CXXForRangeStmt &S);
+  void
+  EmitCXXForRangeStmt(const CXXForRangeStmt &S,
+                      ArrayRef<const Attr *> Attrs = ArrayRef<const Attr *>());
diff --git a/lib/Parse/ParsePragma.cpp b/lib/Parse/ParsePragma.cpp
index 787d3f0..cabba33 100644
--- a/lib/Parse/ParsePragma.cpp
+++ b/lib/Parse/ParsePragma.cpp
@@ -17,0 +18 @@
+#include "clang/Sema/LoopHint.h"
@@ -143,0 +145,6 @@ private:
+struct PragmaLoopHintHandler : public PragmaHandler {
+  PragmaLoopHintHandler() : PragmaHandler("loop") {}
+  void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
+                    Token &FirstToken) override;
+};
+
@@ -210,0 +218,3 @@ void Parser::initializePragmaHandlers() {
+
+  LoopHintHandler.reset(new PragmaLoopHintHandler());
+  PP.AddPragmaHandler(LoopHintHandler.get());
@@ -267,0 +278,3 @@ void Parser::resetPragmaHandlers() {
+
+  PP.RemovePragmaHandler(LoopHintHandler.get());
+  LoopHintHandler.reset();
@@ -588,0 +602,34 @@ unsigned Parser::HandlePragmaMSInitSeg(llvm::StringRef PragmaName,
+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;
+}
+
@@ -1586,0 +1634,104 @@ void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
+
+/// \brief Handle the \#pragma loop directive.
+///  #pragma 'loop' loop-hints
+///
+///  loop-hints:
+///    loop-hint loop-hints[opt]
+///
+///  loop-hint:
+///    'vectorize' '(' loop-hint-value ')'
+///    'interleave' '(' loop-hint-value ')'
+///
+///  loop-hint-value:
+///    'enable'
+///    'disable'
+///    constant-expression
+///
+/// Specifying vectorize(enable) or vectorize(_value_) instructs llvm to
+/// try vectorizing the instructions of the loop it precedes. Specifying
+/// interleave(enable) or interleave(_value_) instructs llvm to try interleaving
+/// multiple iterations of the loop it precedes. The _value_ indicates the
+/// width of the vector instructions or the number of iterations of the loop
+/// that should be interleaved. Consequently, a value of 1 or disable prevents
+/// the optimization, 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)
+        << /*InvalidOption*/ true << "";
+    return;
+  }
+
+  while (Tok.is(tok::identifier)) {
+    Token Option = Tok;
+    StringRef OptionName = Tok.getIdentifierInfo()->getName();
+
+    if (OptionName != "vectorize" && OptionName != "interleave") {
+      PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
+          << /*InvalidOption*/ false << OptionName;
+      return;
+    }
+
+    // Read '('
+    PP.Lex(Tok);
+    if (Tok.isNot(tok::l_paren)) {
+      PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
+      return;
+    }
+
+    PP.Lex(Tok);
+    if (Tok.isNot(tok::identifier) && Tok.isNot(tok::numeric_constant)) {
+      PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_type)
+          << Tok.getName();
+      return;
+    }
+
+    Token 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);
+
+    PragmaLoopHintInfo *Info =
+        (PragmaLoopHintInfo *)PP.getPreprocessorAllocator().Allocate(
+            sizeof(PragmaLoopHintInfo), llvm::alignOf<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) << "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/lib/Parse/ParseStmt.cpp b/lib/Parse/ParseStmt.cpp
index 9d44f51..8534a69 100644
--- a/lib/Parse/ParseStmt.cpp
+++ b/lib/Parse/ParseStmt.cpp
@@ -17,0 +18 @@
+#include "clang/Basic/Attributes.h"
@@ -22,0 +24 @@
+#include "clang/Sema/LoopHint.h"
@@ -359,0 +362,4 @@ Retry:
+
+  case tok::annot_pragma_loop_hint:
+    ProhibitAttributes(Attrs);
+    return ParsePragmaLoopHint(Stmts, OnlyStatement, TrailingElseLoc, Attrs);
@@ -1761,0 +1768,30 @@ StmtResult Parser::ParseReturnStatement() {
+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)};
+    TempAttrs.addNew(Hint.LoopLoc->Ident, Hint.Range, 0, Hint.LoopLoc->Loc,
+                     ArgHints, 3, AttributeList::AS_Pragma);
+  }
+
+  // Get the next statement.
+  MaybeParseCXX11Attributes(Attrs);
+
+  StmtResult S = ParseStatementOrDeclarationAfterAttributes(
+      Stmts, OnlyStatement, TrailingElseLoc, Attrs);
+
+  Attrs.takeAllFrom(TempAttrs);
+  return S;
+}
+
diff --git a/lib/Sema/SemaStmtAttr.cpp b/lib/Sema/SemaStmtAttr.cpp
index 3bc620b..b6f6a6c 100644
--- a/lib/Sema/SemaStmtAttr.cpp
+++ b/lib/Sema/SemaStmtAttr.cpp
@@ -18,0 +19 @@
+#include "clang/Sema/LoopHint.h"
@@ -44,0 +46,74 @@ static Attr *handleFallThroughAttr(Sema &S, Stmt *St, const AttributeList &A,
+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 = LoopHintAttr::Vectorize;
+  if (OptionInfo->getName() == "vectorize")
+    Option = LoopHintAttr::Vectorize;
+  else if (OptionInfo->getName() == "interleave")
+    Option = LoopHintAttr::Interleave;
+
+  LoopHintAttr::ArgType Arg = LoopHintAttr::Value;
+  if (ValueInfo && ValueInfo->getName() == "enable")
+    Arg = LoopHintAttr::Enable;
+  else if (ValueInfo && ValueInfo->getName() == "disable")
+    Arg = LoopHintAttr::Disable;
+
+  // FIXME: We should support template parameters for the loop hint value.
+  // See bug report #19610
+  int ValueInt = 1; // No vectorization/interleaving when kind set to disable.
+  if (Arg == LoopHintAttr::Value) {
+    llvm::APSInt ValueAPS;
+    if (!ValueExpr || !ValueExpr->isIntegerConstantExpr(ValueAPS, S.Context) ||
+        (ValueInt = ValueAPS.getSExtValue()) < 1) {
+      S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_value)
+          << LoopHintAttr::getOptionName(Option);
+      return nullptr;
+    }
+  }
+
+  return LoopHintAttr::CreateImplicit(S.Context, Option, Arg, ValueInt,
+                                      A.getRange());
+}
+
+static void
+CheckForIncompatibleAttributes(Sema &S, SmallVectorImpl<const Attr *> &Attrs) {
+  int PrevArg[2] = {-1, -1};
+
+  for (auto I = Attrs.begin(), E = Attrs.end(); I != E; ++I) {
+    const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(*I);
+
+    // Skip non loop hint attributes
+    if (!LH)
+      continue;
+
+    int Option = LH->getOption();
+    int Arg = LH->getArg();
+    SourceLocation ValueLoc = LH->getRange().getEnd();
+
+    // We only need to check that a loop hint is compatible with the
+    // previous loop hint to ensure that all hints are compatible.
+    if (PrevArg[Option] != -1 &&
+        !LoopHintAttr::isCompatible(PrevArg[Option], Arg)) {
+      S.Diag(ValueLoc, diag::err_pragma_loop_incompatible)
+          << LoopHintAttr::getArgName(PrevArg[Option])
+          << LoopHintAttr::getArgName(Arg)
+          << LoopHintAttr::getOptionName(Option);
+    }
+    PrevArg[Option] = Arg;
+  }
+}
@@ -55,0 +131,2 @@ static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A,
+  case AttributeList::AT_LoopHint:
+    return handleLoopHintAttr(S, St, A, Range);
@@ -72,0 +150,2 @@ StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList,
+  CheckForIncompatibleAttributes(*this, Attrs);
+
diff --git a/test/CodeGen/pragma-loop.cpp b/test/CodeGen/pragma-loop.cpp
new file mode 100644
index 0000000..e793c74
--- /dev/null
+++ b/test/CodeGen/pragma-loop.cpp
@@ -0,0 +1,120 @@
+// RUN: %clang_cc1 -std=c++11 -emit-llvm -o - %s | FileCheck %s
+
+// CHECK: br i1 %cmp, label %while.body, label %while.end, !llvm.loop !1
+// CHECK: br i1 %cmp, label %do.body, label %do.end, !llvm.loop !5
+// CHECK: br i1 %cmp, label %for.body, label %for.end, !llvm.loop !7
+// CHECK: br i1 %cmp, label %for.body, label %for.end, !llvm.loop !8
+// CHECK: br i1 %cmp, label %for.body, label %for.end, !llvm.loop !11
+// CHECK: br i1 %cmp, label %for.body, label %for.end, !llvm.loop !13
+// CHECK: br i1 %cmp, label %for.body, label %for.end, !llvm.loop !14
+// CHECK: br i1 %cmp, label %for.body, label %for.end, !llvm.loop !16
+
+// CHECK: !1 = metadata !{metadata !1, metadata !2, metadata !3, metadata !4}
+// CHECK: !2 = metadata !{metadata !"llvm.vectorizer.width", i32 4}
+// CHECK: !3 = metadata !{metadata !"llvm.vectorizer.unroll", i32 4}
+// CHECK: !4 = metadata !{metadata !"llvm.vectorizer.enable", i1 true}
+// CHECK: !5 = metadata !{metadata !5, metadata !3, metadata !6}
+// CHECK: !6 = metadata !{metadata !"llvm.vectorizer.width", i32 8}
+// CHECK: !7 = metadata !{metadata !7, metadata !3, metadata !4}
+// CHECK: !8 = metadata !{metadata !8, metadata !9, metadata !10}
+// CHECK: !9 = metadata !{metadata !"llvm.vectorizer.unroll", i32 2}
+// CHECK: !10 = metadata !{metadata !"llvm.vectorizer.width", i32 2}
+// CHECK: !11 = metadata !{metadata !11, metadata !12}
+// CHECK: !12 = metadata !{metadata !"llvm.vectorizer.width", i32 1}
+// CHECK: !13 = metadata !{metadata !13, metadata !9, metadata !10}
+// CHECK: !14 = metadata !{metadata !14, metadata !15, metadata !6}
+// CHECK: !15 = metadata !{metadata !"llvm.vectorizer.unroll", i32 8}
+// CHECK: !16 = metadata !{metadata !16, metadata !9, metadata !10}
+
+// Verify while loop is recognized after sequence of pragma loop directives.
+void while_test(int *List, int Length) {
+  int i = 0;
+
+#pragma loop vectorize(enable)
+#pragma loop interleave(4)
+#pragma loop vectorize(4)
+  while (i < Length) {
+    List[i] = i * 2;
+    i++;
+  }
+}
+
+// Verify do loop is recognized after multi-option pragma loop directive.
+void do_test(int *List, int Length) {
+  int i = 0;
+
+#pragma loop vectorize(8) interleave(4)
+  do {
+    List[i] = i * 2;
+    i++;
+  } while (i < Length);
+}
+
+// Verify for loop is recognized after sequence of pragma loop directives.
+void for_test(int *List, int Length) {
+#pragma loop interleave(enable)
+#pragma loop interleave(4)
+  for (int i = 0; i < Length; i++) {
+    List[i] = i * 2;
+  }
+}
+
+// Verify c++11 for range loop is recognized after
+// sequence of pragma loop directives.
+void for_range_test() {
+  double List[100];
+
+#pragma loop vectorize(2) interleave(2)
+  for (int i : List) {
+    List[i] = i;
+  }
+}
+
+// Verify disable pragma loop directive generates correct metadata
+void disable_test(int *List, int Length) {
+#pragma loop vectorize(disable)
+  for (int i = 0; i < Length; i++) {
+    List[i] = i * 2;
+  }
+}
+
+#define VECWIDTH 2
+#define INTERLEAVE 2
+
+// Verify defines are correctly resolved in pragma loop directive
+void for_define_test(int *List, int Length, int Value) {
+#pragma loop vectorize(VECWIDTH) interleave(INTERLEAVE)
+  for (int i = 0; i < Length; i++) {
+    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 loop vectorize(8) interleave(8)
+  for (int i = 0; i < Length; i++) {
+    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 loop vectorize(VECWIDTH) interleave(INTERLEAVE)
+  for (int i = 0; i < Length; i++) {
+    List[i] = i * Value;
+  }
+}
+
+#undef VECWIDTH
+#undef INTERLEAVE
+
+// 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);
+}
diff --git a/test/PCH/pragma-loop.cpp b/test/PCH/pragma-loop.cpp
new file mode 100644
index 0000000..4f852fe
--- /dev/null
+++ b/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 loop interleave(8)
+// CHECK: #pragma loop vectorize(4)
+// CHECK: #pragma loop interleave(disable)
+// CHECK: #pragma loop vectorize(enable)
+// CHECK: #pragma loop interleave(enable)
+// CHECK: #pragma loop vectorize(disable)
+
+#ifndef HEADER
+#define HEADER
+
+class pragma_test {
+public:
+  inline void run1(int *List, int Length) {
+    int i = 0;
+#pragma loop vectorize(4)
+#pragma loop interleave(8)
+    while (i < Length) {
+      List[i] = i;
+      i++;
+    }
+  }
+
+  inline void run2(int *List, int Length) {
+    int i = 0;
+#pragma loop vectorize(enable)
+#pragma loop interleave(disable)
+    while (i - 1 < Length) {
+      List[i] = i;
+      i++;
+    }
+  }
+
+  inline void run3(int *List, int Length) {
+    int i = 0;
+#pragma loop vectorize(disable)
+#pragma 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/test/Parser/pragma-loop-ast.cpp b/test/Parser/pragma-loop-ast.cpp
new file mode 100644
index 0000000..d0f293d
--- /dev/null
+++ b/test/Parser/pragma-loop-ast.cpp
@@ -0,0 +1,35 @@
+// RUN: %clang_cc1 -ast-print %s | 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 loop interleave(8)
+// CHECK: #pragma loop vectorize(4)
+// CHECK: #pragma loop interleave(disable)
+// CHECK: #pragma loop vectorize(enable)
+// CHECK: #pragma loop interleave(enable)
+// CHECK: #pragma loop vectorize(disable)
+
+void test(int *List, int Length) {
+  int i = 0;
+#pragma loop vectorize(4)
+#pragma loop interleave(8)
+  while (i < Length) {
+    List[i] = i * 2;
+    i++;
+  }
+
+#pragma loop vectorize(enable)
+#pragma loop interleave(disable)
+  while (i - 1 < Length) {
+    List[i] = i * 2;
+    i++;
+  }
+
+#pragma loop vectorize(disable)
+#pragma loop interleave(enable)
+  while (i - 2 < Length) {
+    List[i] = i * 2;
+    i++;
+  }
+}
diff --git a/test/Parser/pragma-loop.cpp b/test/Parser/pragma-loop.cpp
new file mode 100644
index 0000000..7c30bd3
--- /dev/null
+++ b/test/Parser/pragma-loop.cpp
@@ -0,0 +1,109 @@
+// RUN: %clang_cc1 -fsyntax-only -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 loop vectorize(4)
+#pragma loop interleave(8)
+  while (i + 1 < Length) {
+    List[i] = i;
+  }
+
+#pragma loop vectorize(enable)
+#pragma loop interleave(enable)
+  while (i < Length) {
+    List[i] = i;
+  }
+
+#pragma loop vectorize(disable)
+#pragma loop interleave(disable)
+  while (i - 1 < Length) {
+    List[i] = i;
+  }
+
+#pragma loop vectorize(4) interleave(8)
+  while (i - 2 < Length) {
+    List[i] = i;
+  }
+
+#pragma loop interleave(16)
+  while (i - 3 < Length) {
+    List[i] = i;
+  }
+
+  int VList[Length];
+#pragma loop vectorize(disable) interleave(disable)
+  for (int j : VList) {
+    VList[j] = List[j];
+  }
+
+/* expected-error {{expected '('}} */ #pragma loop vectorize
+/* expected-error {{expected '('}} */ #pragma loop interleave
+
+/* expected-error {{expected ')'}} */ #pragma loop vectorize(4
+/* expected-error {{expected ')'}} */ #pragma loop interleave(4
+
+/* expected-error {{missing option in directive '#pragma loop'}} */ #pragma loop
+/* expected-error {{invalid option 'badkeyword' in directive '#pragma loop'}} */ #pragma loop badkeyword
+/* expected-error {{invalid option 'badkeyword' in directive '#pragma loop'}} */ #pragma loop badkeyword(2)
+/* expected-error {{invalid option 'badkeyword' in directive '#pragma loop'}} */ #pragma loop vectorize(4) badkeyword(4)
+/* expected-warning {{extra tokens at end of '#pragma loop'}} */ #pragma loop vectorize(4) ,
+
+  while (i-4 < Length) {
+    List[i] = i;
+  }
+
+/* expected-error {{expected a positive integer in directive '#pragma loop vectorize'}} */ #pragma loop vectorize(0)
+/* expected-error {{expected a positive integer in directive '#pragma loop interleave'}} */ #pragma loop interleave(0)
+  while (i-5 < Length) {
+    List[i] = i;
+  }
+
+/* expected-error {{expected a positive integer in directive '#pragma loop vectorize'}} */ #pragma loop vectorize(3000000000)
+/* expected-error {{expected a positive integer in directive '#pragma loop interleave'}} */ #pragma loop interleave(3000000000)
+  while (i-6 < Length) {
+    List[i] = i;
+  }
+
+/* expected-error {{expected a positive integer in directive '#pragma loop vectorize'}} */ #pragma loop vectorize(badvalue)
+/* expected-error {{expected a positive integer in directive '#pragma loop interleave'}} */ #pragma loop interleave(badvalue)
+  while (i-7 < Length) {
+    List[i] = i;
+  }
+
+#pragma loop vectorize(enable)
+/* expected-error {{expected a for, while, or do-while loop to follow the '#pragma 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 {{'disable' and 'value' directive option types are incompatible in '#pragma loop vectorize'}} */ #pragma loop vectorize(4)
+#pragma loop vectorize(disable)
+  while (i-8 < Length) {
+    List[i] = i;
+  }
+
+/* expected-error {{'disable' and 'enable' directive option types are incompatible in '#pragma loop interleave'}} */ #pragma loop interleave(enable)
+#pragma loop interleave(disable)
+  while (i-9 < Length) {
+    List[i] = i;
+  }
+
+/* expected-error {{'value' and 'disable' directive option types are incompatible in '#pragma loop vectorize'}} */ #pragma loop vectorize(disable)
+#pragma loop vectorize(4)
+  while (i-10 < Length) {
+    List[i] = i;
+  }
+
+#pragma loop interleave(enable)
+/* expected-error {{expected statement}} */ }
diff --git a/utils/TableGen/ClangAttrEmitter.cpp b/utils/TableGen/ClangAttrEmitter.cpp
index c409218..c7f235e 100644
--- a/utils/TableGen/ClangAttrEmitter.cpp
+++ b/utils/TableGen/ClangAttrEmitter.cpp
@@ -1056 +1056 @@ writePrettyPrintFunction(Record &R,
-  if (Spellings.size() == 0) {
+  if (Spellings.empty()) {
@@ -1092,0 +1093,3 @@ writePrettyPrintFunction(Record &R,
+    } else if (Variety == "Pragma") {
+      Prefix = "#pragma ";
+      Suffix = "\n";
@@ -1102,0 +1106,8 @@ writePrettyPrintFunction(Record &R,
+    if (Variety == "Pragma") {
+      OS << " \";\n";
+      OS << "    printPrettyPragma(OS, Policy);\n";
+      OS << "    break;\n";
+      OS << "  }\n";
+      continue;
+    }
+
@@ -1779 +1790 @@ void EmitClangAttrHasAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
-  std::vector<Record *> Declspec, GNU;
+  std::vector<Record *> Declspec, GNU, Pragma;
@@ -1792 +1803 @@ void EmitClangAttrHasAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
-      else if (Variety == "CXX11") {
+      else if (Variety == "CXX11")
@@ -1794 +1805,2 @@ void EmitClangAttrHasAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
-      }
+      else if (Variety == "Pragma")
+        Pragma.push_back(R);
@@ -1807,0 +1820,3 @@ void EmitClangAttrHasAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
+  OS << "case AttrSyntax::Pragma:\n";
+  OS << "  return llvm::StringSwitch<bool>(Name)\n";
+  GenerateHasAttrSpellingStringSwitch(Pragma, OS, "Pragma");
@@ -1843,11 +1858,11 @@ void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) {
-      OS << "    if (Name == \""
-        << Spellings[I].name() << "\" && "
-        << "SyntaxUsed == "
-        << StringSwitch<unsigned>(Spellings[I].variety())
-          .Case("GNU", 0)
-          .Case("CXX11", 1)
-          .Case("Declspec", 2)
-          .Case("Keyword", 3)
-          .Default(0)
-        << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n"
-        << "        return " << I << ";\n";
+      OS << "    if (Name == \"" << Spellings[I].name() << "\" && "
+         << "SyntaxUsed == "
+         << StringSwitch<unsigned>(Spellings[I].variety())
+                .Case("GNU", 0)
+                .Case("CXX11", 1)
+                .Case("Declspec", 2)
+                .Case("Keyword", 3)
+                .Case("Pragma", 4)
+                .Default(0)
+         << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n"
+         << "        return " << I << ";\n";
@@ -2473 +2488 @@ void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
-  std::vector<StringMatcher::StringPair> GNU, Declspec, CXX11, Keywords;
+  std::vector<StringMatcher::StringPair> GNU, Declspec, CXX11, Keywords, Pragma;
@@ -2515,0 +2531,2 @@ void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
+        else if (Variety == "Pragma")
+          Matches = &Pragma;
@@ -2539,0 +2557,2 @@ void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
+  OS << "  } else if (AttributeList::AS_Pragma == Syntax) {\n";
+  StringMatcher("Name", Pragma, OS).Emit();
@@ -2645 +2664,2 @@ enum SpellingKind {
-  Keyword = 1 << 3
+  Keyword = 1 << 3,
+  Pragma = 1 << 4
@@ -2690,4 +2710,5 @@ static void WriteDocumentation(const DocumentationData &Doc,
-      .Case("GNU", GNU)
-      .Case("CXX11", CXX11)
-      .Case("Declspec", Declspec)
-      .Case("Keyword", Keyword);
+                            .Case("GNU", GNU)
+                            .Case("CXX11", CXX11)
+                            .Case("Declspec", Declspec)
+                            .Case("Keyword", Keyword)
+                            .Case("Pragma", Pragma);
@@ -2728 +2749,2 @@ static void WriteDocumentation(const DocumentationData &Doc,
-  OS << "   :header: \"GNU\", \"C++11\", \"__declspec\", \"Keyword\"\n\n";
+  OS << "   :header: \"GNU\", \"C++11\", \"__declspec\", \"Keyword\",";
+  OS << " \"Pragma\"\n\n";
@@ -2736,0 +2759,2 @@ static void WriteDocumentation(const DocumentationData &Doc,
+  OS << "\"\n\n";
+  if (SupportedSpellings & Pragma) OS << "X";
