|
| 1 | +//===------------- HostPipes.cpp - SYCL Host Pipes Pass -------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// See comments in the header. |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | + |
| 11 | +#include "llvm/SYCLLowerIR/HostPipes.h" |
| 12 | +#include "llvm/SYCLLowerIR/CompileTimePropertiesPass.h" |
| 13 | +#include "llvm/SYCLLowerIR/DeviceGlobals.h" |
| 14 | + |
| 15 | +#include "llvm/ADT/STLExtras.h" |
| 16 | +#include "llvm/ADT/StringRef.h" |
| 17 | +#include "llvm/IR/Module.h" |
| 18 | + |
| 19 | +#include <cassert> |
| 20 | + |
| 21 | +using namespace llvm; |
| 22 | + |
| 23 | +namespace { |
| 24 | + |
| 25 | +constexpr StringRef SYCL_HOST_PIPE_ATTR = "sycl-host-pipe"; |
| 26 | +constexpr StringRef SYCL_HOST_PIPE_SIZE_ATTR = "sycl-host-pipe-size"; |
| 27 | + |
| 28 | +/// Returns the size (in bytes) of the type \c T of the host |
| 29 | +/// pipe variable. |
| 30 | +/// |
| 31 | +/// The function gets this value from the LLVM IR attribute \c |
| 32 | +/// sycl-host-pipe-size. |
| 33 | +/// |
| 34 | +/// @param GV [in] Host Pipe variable. |
| 35 | +/// |
| 36 | +/// @returns the size (int bytes) of the underlying type \c T of the |
| 37 | +/// host pipe variable represented in the LLVM IR by @GV. |
| 38 | +uint32_t getHostPipeTypeSize(const GlobalVariable &GV) { |
| 39 | + assert(GV.hasAttribute(SYCL_HOST_PIPE_SIZE_ATTR) && |
| 40 | + "The host pipe variable must have the 'sycl-host-pipe-size' " |
| 41 | + "attribute that must contain a number representing the size of the " |
| 42 | + "underlying type T of the host pipe variable"); |
| 43 | + return getAttributeAsInteger<uint32_t>(GV, SYCL_HOST_PIPE_SIZE_ATTR); |
| 44 | +} |
| 45 | + |
| 46 | +} // anonymous namespace |
| 47 | + |
| 48 | +namespace llvm { |
| 49 | + |
| 50 | +/// Return \c true if the variable @GV is a host pipe variable. |
| 51 | +/// |
| 52 | +/// The function checks whether the variable has the LLVM IR attribute \c |
| 53 | +/// sycl-host-pipe. |
| 54 | +/// @param GV [in] A variable to test. |
| 55 | +/// |
| 56 | +/// @return \c true if the variable is a host pipe variable, \c false |
| 57 | +/// otherwise. |
| 58 | +bool isHostPipeVariable(const GlobalVariable &GV) { |
| 59 | + return GV.hasAttribute(SYCL_HOST_PIPE_ATTR); |
| 60 | +} |
| 61 | + |
| 62 | +HostPipePropertyMapTy collectHostPipeProperties(const Module &M) { |
| 63 | + HostPipePropertyMapTy HPM; |
| 64 | + auto HostPipeNum = count_if(M.globals(), isHostPipeVariable); |
| 65 | + if (HostPipeNum == 0) |
| 66 | + return HPM; |
| 67 | + |
| 68 | + HPM.reserve(HostPipeNum); |
| 69 | + |
| 70 | + for (auto &GV : M.globals()) { |
| 71 | + if (!isHostPipeVariable(GV)) |
| 72 | + continue; |
| 73 | + |
| 74 | + HPM[getGlobalVariableUniqueId(GV)] = {getHostPipeTypeSize(GV)}; |
| 75 | + } |
| 76 | + |
| 77 | + return HPM; |
| 78 | +} |
| 79 | + |
| 80 | +} // namespace llvm |
0 commit comments