[StreamExecutor] Add Platform and PlatformManager

Summary: Abstractions for a StreamExecutor platform

Reviewers: jlebar

Subscribers: jprice, parallel_libs-commits

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

llvm-svn: 279779
GitOrigin-RevId: 20cf1eb16106bdb0b8787e55df9dfb63ae86464e
diff --git a/streamexecutor/include/streamexecutor/Platform.h b/streamexecutor/include/streamexecutor/Platform.h
new file mode 100644
index 0000000..674a40c
--- /dev/null
+++ b/streamexecutor/include/streamexecutor/Platform.h
@@ -0,0 +1,42 @@
+//===-- Platform.h - The Platform class -------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// The Platform class which represents a platform such as CUDA or OpenCL.
+///
+/// This is an abstract base class that will be overridden by each specific
+/// platform.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef STREAMEXECUTOR_PLATFORM_H
+#define STREAMEXECUTOR_PLATFORM_H
+
+#include "streamexecutor/Utils/Error.h"
+
+namespace streamexecutor {
+
+class Device;
+
+class Platform {
+public:
+  virtual ~Platform();
+
+  /// Gets the number of devices available for this platform.
+  virtual size_t getDeviceCount() const = 0;
+
+  /// Gets a pointer to a Device with the given index for this platform.
+  ///
+  /// Ownership of the Device instance is NOT transferred to the caller.
+  virtual Expected<Device *> getDevice(size_t DeviceIndex) = 0;
+};
+
+} // namespace streamexecutor
+
+#endif // STREAMEXECUTOR_PLATFORM_H
diff --git a/streamexecutor/include/streamexecutor/PlatformManager.h b/streamexecutor/include/streamexecutor/PlatformManager.h
new file mode 100644
index 0000000..c8f68fc
--- /dev/null
+++ b/streamexecutor/include/streamexecutor/PlatformManager.h
@@ -0,0 +1,53 @@
+//===-- PlatformManager.h - The PlatformManager class -----------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// PlatformManager is the entry point into the StreamExecutor API. A user
+/// begins be calling PlatformManager::getPlatformByName("cuda") where "cuda"
+/// can be replaced by any supported platform name. This gives the user a
+/// Platform object that can be used to create Device objects for that platform,
+/// etc.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef STREAMEXECUTOR_PLATFORMMANAGER_H
+#define STREAMEXECUTOR_PLATFORMMANAGER_H
+
+#include <map>
+
+#include "streamexecutor/Platform.h"
+#include "streamexecutor/Utils/Error.h"
+
+namespace streamexecutor {
+
+/// A singleton that holds a reference to a Platform object for each
+/// supported StreamExecutor platform.
+class PlatformManager {
+public:
+  /// Gets a reference to the Platform with the given name.
+  ///
+  /// The name parameter is not case-sensitive, so the following arguments are
+  /// all equivalent: "cuda", "CUDA", "Cuda", "cUdA".
+  ///
+  /// Returns an error if no platform is present for the name.
+  ///
+  /// Ownership of the platform is NOT transferred to the caller.
+  static Expected<Platform *> getPlatformByName(llvm::StringRef Name);
+
+private:
+  PlatformManager();
+  PlatformManager(const PlatformManager &) = delete;
+  PlatformManager operator=(const PlatformManager &) = delete;
+
+  std::map<std::string, std::unique_ptr<Platform>> PlatformsByName;
+};
+
+} // namespace streamexecutor
+
+#endif // STREAMEXECUTOR_PLATFORMMANAGER_H
diff --git a/streamexecutor/lib/CMakeLists.txt b/streamexecutor/lib/CMakeLists.txt
index cf7baf9..f33e346 100644
--- a/streamexecutor/lib/CMakeLists.txt
+++ b/streamexecutor/lib/CMakeLists.txt
@@ -10,7 +10,9 @@
     Kernel.cpp
     KernelSpec.cpp
     PackedKernelArgumentArray.cpp
+    Platform.cpp
     PlatformInterfaces.cpp
+    PlatformManager.cpp
     Stream.cpp)
 target_link_libraries(streamexecutor ${llvm_libs})
 
diff --git a/streamexecutor/lib/Platform.cpp b/streamexecutor/lib/Platform.cpp
new file mode 100644
index 0000000..4250468
--- /dev/null
+++ b/streamexecutor/lib/Platform.cpp
@@ -0,0 +1,21 @@
+//===-- Platform.cpp - Platform implementation ----------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of Platform class internals.
+///
+//===----------------------------------------------------------------------===//
+
+#include "streamexecutor/Platform.h"
+
+namespace streamexecutor {
+
+Platform::~Platform() = default;
+
+} // namespace streamexecutor
diff --git a/streamexecutor/lib/PlatformManager.cpp b/streamexecutor/lib/PlatformManager.cpp
new file mode 100644
index 0000000..9cae5b1
--- /dev/null
+++ b/streamexecutor/lib/PlatformManager.cpp
@@ -0,0 +1,36 @@
+//===-- PlatformManager.cpp - PlatformManager implementation --------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of PlatformManager class internals.
+///
+//===----------------------------------------------------------------------===//
+
+#include "streamexecutor/PlatformManager.h"
+
+namespace streamexecutor {
+
+PlatformManager::PlatformManager() {
+  // TODO(jhen): Register known platforms by name.
+  // We have a couple of options here:
+  //  * Use build-system flags to set preprocessor macros that select the
+  //    appropriate code to include here.
+  //  * Use static initialization tricks to have platform libraries register
+  //    themselves when they are loaded.
+}
+
+Expected<Platform *> PlatformManager::getPlatformByName(llvm::StringRef Name) {
+  static PlatformManager Instance;
+  auto Iterator = Instance.PlatformsByName.find(Name.lower());
+  if (Iterator != Instance.PlatformsByName.end())
+    return Iterator->second.get();
+  return make_error("no available platform with name " + Name);
+}
+
+} // namespace streamexecutor