Add OpenCL 2.0 atomic builtin functions as Clang builtin

OpenCL 2.0 atomic builtin functions have a scope argument which is ideally
represented as synchronization scope argument in LLVM atomic instructions.

Clang supports translating Clang atomic builtin functions to LLVM atomic
instructions. However it currently does not support synchronization scope
of LLVM atomic instructions. Without this, users have to use LLVM assembly
code to implement OpenCL atomic builtin functions.

This patch adds OpenCL 2.0 atomic builtin functions as Clang builtin
functions, which supports generating LLVM atomic instructions with
synchronization scope operand.

Currently only constant memory scope argument is supported. Support of
non-constant memory scope argument will be added later.

Differential Revision: https://reviews.llvm.org/D28691

llvm-svn: 310082
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp
index 64128df..493144f 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -14,6 +14,7 @@
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/MacroBuilder.h"
 #include "clang/Basic/SourceManager.h"
+#include "clang/Basic/SyncScope.h"
 #include "clang/Basic/TargetInfo.h"
 #include "clang/Basic/Version.h"
 #include "clang/Frontend/FrontendDiagnostic.h"
@@ -575,6 +576,18 @@
   Builder.defineMacro("__ATOMIC_ACQ_REL", "4");
   Builder.defineMacro("__ATOMIC_SEQ_CST", "5");
 
+  // Define macros for the OpenCL memory scope.
+  // The values should match clang SyncScope enum.
+  assert(static_cast<unsigned>(SyncScope::OpenCLWorkGroup) == 1 &&
+         static_cast<unsigned>(SyncScope::OpenCLDevice) == 2 &&
+         static_cast<unsigned>(SyncScope::OpenCLAllSVMDevices) == 3 &&
+         static_cast<unsigned>(SyncScope::OpenCLSubGroup) == 4);
+  Builder.defineMacro("__OPENCL_MEMORY_SCOPE_WORK_ITEM", "0");
+  Builder.defineMacro("__OPENCL_MEMORY_SCOPE_WORK_GROUP", "1");
+  Builder.defineMacro("__OPENCL_MEMORY_SCOPE_DEVICE", "2");
+  Builder.defineMacro("__OPENCL_MEMORY_SCOPE_ALL_SVM_DEVICES", "3");
+  Builder.defineMacro("__OPENCL_MEMORY_SCOPE_SUB_GROUP", "4");
+
   // Support for #pragma redefine_extname (Sun compatibility)
   Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1");