Propagating prior merge from 'llvm.org/release_40'.
diff --git a/docs/ReleaseNotes.rst b/docs/ReleaseNotes.rst
index 25ea577..08d7a75 100644
--- a/docs/ReleaseNotes.rst
+++ b/docs/ReleaseNotes.rst
@@ -17,7 +17,7 @@
 Introduction
 ============
 
-This document contains the release notes for the Clang C/C++/Objective-C
+This document contains the release notes for the Clang C/C++/Objective-C/OpenCL
 frontend, part of the LLVM Compiler Infrastructure, release 4.0.0. Here we
 describe the status of Clang in some detail, including major
 improvements from the previous release and new feature work. For the
@@ -139,7 +139,76 @@
 OpenCL C Language Changes in Clang
 ----------------------------------
 
-...
+**The following bugs in the OpenCL header have been fixed:**
+
+* Added missing ``overloadable`` and ``convergent`` attributes.
+* Removed some erroneous extra ``native_*`` functions.
+
+**The following bugs in the generation of metadata have been fixed:**
+
+* Corrected the SPIR version depending on the OpenCL version.
+* Source level address spaces are taken from the SPIR specification.
+* Image types now contain no access qualifier.
+
+**The following bugs in the AMD target have been fixed:**
+
+* Corrected the bitwidth of ``size_t`` and NULL pointer value with respect to
+  address spaces.
+* Added ``cl_khr_subgroups``, ``cl_amd_media_ops`` and ``cl_amd_media_ops2``
+  extensions.
+* Added ``cl-denorms-are-zero`` support.
+* Changed address spaces for image objects to be ``constant``.
+* Added little-endian.
+
+**The following bugs in OpenCL 2.0 have been fixed:**
+
+* Fixed pipe builtin function return type, added extra argument to generated
+  IR intrinsics to propagate size and alignment information of the pipe packed
+  type.
+* Improved pipe type to accommodate access qualifiers.
+* Added correct address space to the ObjC block generation and ``enqueue_kernel``
+  prototype.
+* Improved handling of integer parameters of ``enqueue_kernel`` prototype. We
+  now allow ``size_t`` instead of ``int`` for specifying block parameter sizes.
+* Allow using NULL (aka ``CLK_NULL_QUEUE``) with ``queue_t``.
+
+
+**Improved the following diagnostics:**
+
+* Disallow address spaces other than ``global`` for kernel pointer parameters.
+* Correct the use of half type argument and pointer assignment with
+  dereferencing.
+* Disallow variadic arguments in functions and blocks.
+* Allow partial initializer for array and struct.
+
+**Some changes to OpenCL extensions have been made:**
+
+* Added ``cl_khr_mipmap_image``.
+* Added ``-cl-ext`` flag to allow overwriting supported extensions otherwise
+  set by the target compiled for (Example: ``-cl-ext=-all,+cl_khr_fp16``).
+* New types and functions can now be flexibly added to extensions using the
+  following pragmas instead of modifying the Clang source code:
+
+  .. code-block:: c
+
+       #pragma OPENCL EXTENSION the_new_extension_name : begin
+       // declare types and functions associated with the extension here
+       #pragma OPENCL EXTENSION the_new_extension_name : end
+
+
+**Miscellaneous changes:**
+
+* Fix ``__builtin_astype`` to cast between different address space objects.
+* Allow using ``opencl_unroll_hint`` with earlier OpenCL versions than 2.0.
+* Improved handling of floating point literal to default to single precision if
+  fp64 extension is not enabled.
+* Refactor ``sampler_t`` implementation to simplify initializer representation
+  which is now handled as a compiler builtin function with an integer value
+  passed into it.
+* Change fake address space map to use the SPIR convention.
+* Added `the OpenCL manual
+  <https://clang.llvm.org/docs/UsersManual.html#opencl-features>`_ to Clang
+  documentation.
 
 OpenMP Support in Clang
 ----------------------------------
diff --git a/docs/UsersManual.rst b/docs/UsersManual.rst
index 04023dd..6c83557 100644
--- a/docs/UsersManual.rst
+++ b/docs/UsersManual.rst
@@ -2056,6 +2056,8 @@
 In this case the kernel code should contain ``#include <opencl-c.h>`` just as a
 regular C include.
 
+.. _opencl_cl_ext:
+
 .. option:: -cl-ext
 
 Disables support of OpenCL extensions. All OpenCL targets provide a list
@@ -2177,6 +2179,41 @@
 
      $ clang -target spir-unknown-unknown -c -emit-llvm -Xclang -finclude-default-header -fmodules -fimplicit-module-maps -fmodules-cache-path=<path to the generated module> test.cl
 
+OpenCL Extensions
+-----------------
+
+All of the ``cl_khr_*`` extensions from `the official OpenCL specification
+<https://www.khronos.org/registry/OpenCL/sdk/2.0/docs/man/xhtml/EXTENSION.html>`_
+up to and including version 2.0 are available and set per target depending on the
+support available in the specific architecture.
+
+It is possible to alter the default extensions setting per target using
+``-cl-ext`` flag. (See :ref:`flags description <opencl_cl_ext>` for more details).
+
+Vendor extensions can be added flexibly by declaring the list of types and
+functions associated with each extensions enclosed within the following
+compiler pragma directives:
+
+  .. code-block:: c
+
+       #pragma OPENCL EXTENSION the_new_extension_name : begin
+       // declare types and functions associated with the extension here
+       #pragma OPENCL EXTENSION the_new_extension_name : end
+
+For example, parsing the following code adds ``my_t`` type and ``my_func``
+function to the custom ``my_ext`` extension.
+
+  .. code-block:: c
+
+       #pragma OPENCL EXTENSION my_ext : begin
+       typedef struct{
+         int a;
+       }my_t;
+       void my_func(my_t);
+       #pragma OPENCL EXTENSION my_ext : end
+
+Declaring the same types in different vendor extensions is disallowed.
+
 OpenCL Metadata
 ---------------
 
@@ -2215,7 +2252,7 @@
 <https://www.khronos.org/registry/cl/specs/opencl-2.0-openclc.pdf#49>`_
 
 
-opencl_hint_unroll
+opencl_unroll_hint
 ^^^^^^^^^^^^^^^^^^
 
 The implementation of this feature mirrors the unroll hint for C.
diff --git a/lib/Parse/ParseExpr.cpp b/lib/Parse/ParseExpr.cpp
index ee06c76..852e226 100644
--- a/lib/Parse/ParseExpr.cpp
+++ b/lib/Parse/ParseExpr.cpp
@@ -2408,7 +2408,7 @@
       // fold-expressions, we'll need to allow multiple ArgExprs here.
       if (ArgExprs.size() == 1 && isFoldOperator(Tok.getKind()) &&
           NextToken().is(tok::ellipsis))
-        return ParseFoldExpression(Result, T);
+        return ParseFoldExpression(ArgExprs[0], T);
 
       ExprType = SimpleExpr;
       Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
diff --git a/lib/Sema/SemaTemplateVariadic.cpp b/lib/Sema/SemaTemplateVariadic.cpp
index 54556b5..725a3e4 100644
--- a/lib/Sema/SemaTemplateVariadic.cpp
+++ b/lib/Sema/SemaTemplateVariadic.cpp
@@ -1014,6 +1014,11 @@
   CheckFoldOperand(*this, LHS);
   CheckFoldOperand(*this, RHS);
 
+  auto DiscardOperands = [&] {
+    CorrectDelayedTyposInExpr(LHS);
+    CorrectDelayedTyposInExpr(RHS);
+  };
+
   // [expr.prim.fold]p3:
   //   In a binary fold, op1 and op2 shall be the same fold-operator, and
   //   either e1 shall contain an unexpanded parameter pack or e2 shall contain
@@ -1021,6 +1026,7 @@
   if (LHS && RHS &&
       LHS->containsUnexpandedParameterPack() ==
           RHS->containsUnexpandedParameterPack()) {
+    DiscardOperands();
     return Diag(EllipsisLoc,
                 LHS->containsUnexpandedParameterPack()
                     ? diag::err_fold_expression_packs_both_sides
@@ -1034,6 +1040,7 @@
   if (!LHS || !RHS) {
     Expr *Pack = LHS ? LHS : RHS;
     assert(Pack && "fold expression with neither LHS nor RHS");
+    DiscardOperands();
     if (!Pack->containsUnexpandedParameterPack())
       return Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
              << Pack->getSourceRange();
diff --git a/test/Parser/cxx1z-fold-expressions.cpp b/test/Parser/cxx1z-fold-expressions.cpp
index 0306385..b1f7318 100644
--- a/test/Parser/cxx1z-fold-expressions.cpp
+++ b/test/Parser/cxx1z-fold-expressions.cpp
@@ -34,3 +34,12 @@
 template<int ...N> int bad10() { return (3 ? ... : N); } // expected-error +{{}} expected-note {{to match}}
 template<int ...N> int bad11() { return (N + ... 0); } // expected-error {{expected a foldable binary operator}} expected-error {{expected expression}}
 template<int ...N> int bad12() { return (... N); } // expected-error {{expected expression}}
+
+template<typename ...T> void as_operand_of_cast(int a, T ...t) {
+  return
+    (int)(a + ... + undeclared_junk) + // expected-error {{undeclared}} expected-error {{does not contain any unexpanded}}
+    (int)(t + ... + undeclared_junk) + // expected-error {{undeclared}}
+    (int)(... + undeclared_junk) + // expected-error {{undeclared}} expected-error {{does not contain any unexpanded}}
+    (int)(undeclared_junk + ...) + // expected-error {{undeclared}}
+    (int)(a + ...); // expected-error {{does not contain any unexpanded}}
+}