[clang] [llvm] [SYCL] Add offload wrapping for SYCL kind. (PR #147508)

2025-08-29 Thread Yury Plyakhin via cfe-commits


@@ -717,6 +717,13 @@ wrapDeviceImages(ArrayRef> 
Buffers,
 M, BuffersToWrap.front(), offloading::getOffloadEntryArray(M)))
   return std::move(Err);
 break;
+  case OFK_SYCL: {
+offloading::SYCLWrappingOptions WrappingOptions;

YuriPlyakhin wrote:

It seems `WrappingOptions` is not used currently. If there is a plan to use it 
in the following patches, might be good to add some comment about it.

https://github.com/llvm/llvm-project/pull/147508
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add offload wrapping for SYCL kind. (PR #147508)

2025-08-29 Thread Yury Plyakhin via cfe-commits


@@ -52,6 +54,23 @@ LLVM_ABI llvm::Error wrapHIPBinary(llvm::Module &M, 
llvm::ArrayRef Images,
EntryArrayTy EntryArray,
llvm::StringRef Suffix = "",
bool EmitSurfacesAndTextures = true);
+
+struct SYCLWrappingOptions {
+  // target/compiler specific options what are suggested to use to "compile"

YuriPlyakhin wrote:

```suggestion
  // Target/compiler specific options that are suggested to use to "compile"
```

https://github.com/llvm/llvm-project/pull/147508
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add offload wrapping for SYCL kind. (PR #147508)

2025-08-29 Thread Yury Plyakhin via cfe-commits

https://github.com/YuriPlyakhin edited 
https://github.com/llvm/llvm-project/pull/147508
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add offload wrapping for SYCL kind. (PR #147508)

2025-08-29 Thread Yury Plyakhin via cfe-commits


@@ -52,6 +54,23 @@ LLVM_ABI llvm::Error wrapHIPBinary(llvm::Module &M, 
llvm::ArrayRef Images,
EntryArrayTy EntryArray,
llvm::StringRef Suffix = "",
bool EmitSurfacesAndTextures = true);
+
+struct SYCLWrappingOptions {
+  // target/compiler specific options what are suggested to use to "compile"
+  // program at runtime.
+  std::string CompileOptions;
+  // Target/Compiler specific options that are suggested to use to "link"

YuriPlyakhin wrote:

nit
```suggestion
  // Target/compiler specific options that are suggested to use to "link"
```

https://github.com/llvm/llvm-project/pull/147508
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [SYCL] Add offload wrapping for SYCL kind. (PR #147508)

2025-09-08 Thread Yury Plyakhin via cfe-commits


@@ -620,6 +635,422 @@ void createRegisterFatbinFunction(Module &M, 
GlobalVariable *FatbinDesc,
   // Add this function to constructors.
   appendToGlobalCtors(M, CtorFunc, /*Priority=*/101);
 }
+
+/// SYCLWrapper helper class that creates all LLVM IRs wrapping given images.
+struct SYCLWrapper {
+  Module &M;
+  LLVMContext &C;
+  SYCLWrappingOptions Options;
+
+  StructType *EntryTy = nullptr;
+  StructType *SyclDeviceImageTy = nullptr;
+  StructType *SyclBinDescTy = nullptr;
+
+  SYCLWrapper(Module &M, const SYCLWrappingOptions &Options)
+  : M(M), C(M.getContext()), Options(Options) {
+EntryTy = offloading::getEntryTy(M);
+SyclDeviceImageTy = getSyclDeviceImageTy();
+SyclBinDescTy = getSyclBinDescTy();
+  }
+
+  IntegerType *getSizeTTy() {
+switch (M.getDataLayout().getPointerSize()) {
+case 4:
+  return Type::getInt32Ty(C);
+case 8:
+  return Type::getInt64Ty(C);
+}
+llvm_unreachable("unsupported pointer type size");
+  }
+
+  SmallVector getSizetConstPair(size_t First, size_t Second) {
+IntegerType *SizeTTy = getSizeTTy();
+return SmallVector{ConstantInt::get(SizeTTy, First),
+  ConstantInt::get(SizeTTy, Second)};
+  }
+
+  /// Note: Properties aren't supported and the support is going
+  /// to be added later.
+  /// Creates a structure corresponding to:
+  /// SYCL specific image descriptor type.
+  /// \code
+  /// struct __sycl.tgt_device_image {
+  ///   // version of this structure - for backward compatibility;
+  ///   // all modifications which change order/type/offsets of existing fields
+  ///   // should increment the version.
+  ///   uint16_t Version;
+  ///   // the kind of offload model the image employs.
+  ///   uint8_t OffloadKind;
+  ///   // format of the image data - SPIRV, LLVMIR bitcode, etc
+  ///   uint8_t Format;
+  ///   // null-terminated string representation of the device's target
+  ///   // architecture
+  ///   const char *Arch;
+  ///   // a null-terminated string; target- and compiler-specific options
+  ///   // which are suggested to use to "compile" program at runtime
+  ///   const char *CompileOptions;
+  ///   // a null-terminated string; target- and compiler-specific options
+  ///   // which are suggested to use to "link" program at runtime
+  ///   const char *LinkOptions;
+  ///   // Pointer to the device binary image start
+  ///   void *ImageStart;
+  ///   // Pointer to the device binary image end
+  ///   void *ImageEnd;
+  ///   // the entry table
+  ///   __tgt_offload_entry *EntriesBegin;
+  ///   __tgt_offload_entry *EntriesEnd;
+  ///   const char *PropertiesBegin;
+  ///   const char *PropertiesEnd;
+  /// };
+  /// \endcode
+  StructType *getSyclDeviceImageTy() {
+return StructType::create(
+{
+Type::getInt16Ty(C),   // Version
+Type::getInt8Ty(C),// OffloadKind
+Type::getInt8Ty(C),// Format
+PointerType::getUnqual(C), // Arch
+PointerType::getUnqual(C), // CompileOptions
+PointerType::getUnqual(C), // LinkOptions
+PointerType::getUnqual(C), // ImageStart
+PointerType::getUnqual(C), // ImageEnd
+PointerType::getUnqual(C), // EntriesBegin
+PointerType::getUnqual(C), // EntriesEnd
+PointerType::getUnqual(C), // PropertiesBegin
+PointerType::getUnqual(C)  // PropertiesEnd
+},
+"__sycl.tgt_device_image");
+  }
+
+  /// Creates a structure for SYCL specific binary descriptor type. Corresponds
+  /// to:
+  ///
+  /// \code
+  ///  struct __sycl.tgt_bin_desc {
+  ///// version of this structure - for backward compatibility;
+  ///// all modifications which change order/type/offsets of existing 
fields
+  ///// should increment the version.
+  ///uint16_t Version;
+  ///uint16_t NumDeviceImages;
+  ///__sycl.tgt_device_image *DeviceImages;
+  ///// the offload entry table
+  ///__tgt_offload_entry *HostEntriesBegin;
+  ///__tgt_offload_entry *HostEntriesEnd;
+  ///  };
+  /// \endcode
+  StructType *getSyclBinDescTy() {
+return StructType::create(
+{Type::getInt16Ty(C), Type::getInt16Ty(C), PointerType::getUnqual(C),
+ PointerType::getUnqual(C), PointerType::getUnqual(C)},
+"__sycl.tgt_bin_desc");
+  }
+
+  /// Adds a global readonly variable that is initialized by given
+  /// \p Initializer to the module.
+  GlobalVariable *addGlobalArrayVariable(const Twine &Name,
+ ArrayRef Initializer,
+ const Twine &Section = "") {
+auto *Arr = ConstantDataArray::get(M.getContext(), Initializer);
+auto *Var = new GlobalVariable(M, Arr->getType(), /*isConstant*/ true,
+   GlobalVariable::InternalLinkage, Arr, Name);
+Var->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
+
+SmallVect