[llvm] [clang] [mlir] [clang-tools-extra] [MLIR] Enable GPU Dialect to SYCL runtime integration (PR #71430)

2023-11-13 Thread Fabian Mora via cfe-commits


@@ -4,6 +4,7 @@
 module attributes {gpu.container_module} {
   // CHECK: [[ARGS_TY:%.*]] = type { i32, i32 }
   // CHECK: @kernel_module_bin_cst = internal constant [4 x i8] c"BLOB", align 
8
+  // CHECK:  @kernel_module_bin_size_cst = internal constant i64 4, align 8

fabianmcg wrote:

Please update the tests with the new changes.

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


[clang] [llvm] [clang-tools-extra] [mlir] [MLIR] Enable GPU Dialect to SYCL runtime integration (PR #71430)

2023-11-13 Thread Fabian Mora via cfe-commits


@@ -377,10 +379,17 @@ 
llvm::LaunchKernel::createKernelLaunch(mlir::gpu::LaunchFuncOp op,
   if (!binary)
 return op.emitError() << "Couldn't find the binary: " << binaryIdentifier;
 
+  auto binaryVar = dyn_cast(binary);
+  llvm::Constant *binaryInit = binaryVar->getInitializer();
+  auto binaryDataSeq = dyn_cast(binaryInit);

fabianmcg wrote:

Some form of check must be placed on the pointers:
```suggestion
  auto binaryVar = dyn_cast(binary);
  assert(binaryVar && "expected a global variable");
  llvm::Constant *binaryInit = binaryVar->getInitializer();
  auto binaryDataSeq = 
dyn_cast_or_null(binaryInit);
  assert(binaryDataSeq && "expected a valid initializer");
```

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


[flang] [libcxx] [lldb] [clang] [compiler-rt] [lld] [mlir] [libc] [clang-tools-extra] [openmp] [llvm] [MLIR] Enable GPU Dialect to SYCL runtime integration (PR #71430)

2023-12-05 Thread Fabian Mora via cfe-commits

https://github.com/fabianmcg approved this pull request.

LGTM! I'll squash and merge as soon it passes all pre check tests.

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


[clang] [libc] [compiler-rt] [libcxx] [lld] [mlir] [clang-tools-extra] [llvm] [flang] [openmp] [lldb] [MLIR] Enable GPU Dialect to SYCL runtime integration (PR #71430)

2023-12-05 Thread Fabian Mora via cfe-commits

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


[libcxx] [mlir] [clang-tools-extra] [lld] [libc] [openmp] [flang] [compiler-rt] [llvm] [clang] [lldb] [MLIR] Enable GPU Dialect to SYCL runtime integration (PR #71430)

2023-12-06 Thread Fabian Mora via cfe-commits


@@ -0,0 +1,31 @@
+//===- SPIRVToLLVMIRTranslation.cpp - Translate SPIRV to LLVM IR 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This file implements a translation between the MLIR SPIRV dialect and
+// LLVM IR.
+//
+//===--===//
+
+#include "mlir/Target/LLVMIR/Dialect/SPIRV/SPIRVToLLVMIRTranslation.h"
+#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
+#include "mlir/IR/BuiltinAttributes.h"
+#include "mlir/IR/Operation.h"
+#include "mlir/Target/LLVMIR/ModuleTranslation.h"
+
+using namespace mlir;
+using namespace mlir::LLVM;
+
+void mlir::registerSPIRVDialectTranslation(DialectRegistry ®istry) {
+  registry.insert();

fabianmcg wrote:

I'm sorry I missed these. I had the same concern, the alternative was adding an 
inline registration call. However, I then realized that adding `#include 
"mlir/Dialect/SPIRV/IR/SPIRVDialect.h"` to `mlir/Target/LLVMIR/Dialect/All.h` 
was needed , which seemed even more undesirable thus I changed my mind and 
agreed on the current scheme. 
One way to clean it, is removing inline function from 
`mlir/Target/LLVMIR/Dialect/All.h` and creating a `MLIRToLLVMIR` library.

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


[lld] [mlir] [llvm] [clang-tools-extra] [flang] [compiler-rt] [libc] [libcxx] [lldb] [clang] [MLIR] Enable GPU Dialect to SYCL runtime integration (PR #71430)

2023-11-17 Thread Fabian Mora via cfe-commits


@@ -61,6 +63,7 @@ registerAllGPUToLLVMIRTranslations(DialectRegistry ®istry) 
{
   registerLLVMDialectTranslation(registry);
   registerNVVMDialectTranslation(registry);
   registerROCDLDialectTranslation(registry);
+  registerSPIRVDialectTranslation(registry);

fabianmcg wrote:

The call to `registry.insert();` is needed so that 
`mlir-translate` can parse the code containing the SPIR-V target attribute, 
nothing more; there's no translation happening from SPIR-V to LLVM. If the call 
is not added, then `mlir-translate` throws an error because `SPIR-V` never gets 
registered.

The question is, should an empty translation to LLVM should be added to mirror 
all other * to LLVM translation code structure, or is inlining the call ok? I 
definitely prefer the second option -one less target.

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


[libc] [clang-tools-extra] [mlir] [lldb] [libcxx] [llvm] [compiler-rt] [flang] [clang] [lld] [MLIR] Enable GPU Dialect to SYCL runtime integration (PR #71430)

2023-11-17 Thread Fabian Mora via cfe-commits

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


[flang] [mlir] [clang-tools-extra] [lld] [compiler-rt] [lldb] [llvm] [libc] [libcxx] [clang] [MLIR] Enable GPU Dialect to SYCL runtime integration (PR #71430)

2023-11-17 Thread Fabian Mora via cfe-commits

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


[compiler-rt] [lld] [libcxx] [clang-tools-extra] [clang] [llvm] [libc] [lldb] [mlir] [flang] [MLIR] Enable GPU Dialect to SYCL runtime integration (PR #71430)

2023-11-20 Thread Fabian Mora via cfe-commits


@@ -61,6 +63,7 @@ registerAllGPUToLLVMIRTranslations(DialectRegistry ®istry) 
{
   registerLLVMDialectTranslation(registry);
   registerNVVMDialectTranslation(registry);
   registerROCDLDialectTranslation(registry);
+  registerSPIRVDialectTranslation(registry);

fabianmcg wrote:

@silee2 can you add a test 
[here](https://github.com/llvm/llvm-project/blob/main/mlir/test/Target/LLVMIR/gpu.mlir#L46-L49)
 using the GPU SPIR-V target attribute?

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


[clang-tools-extra] [libc] [libcxx] [compiler-rt] [flang] [mlir] [clang] [llvm] [mlir][spirv] Implement gpu::TargetAttrInterface (PR #69949)

2023-11-05 Thread Fabian Mora via cfe-commits


@@ -15,6 +15,7 @@
 
 #include "Utils.h"
 #include "mlir/Dialect/GPU/IR/GPUDialect.h"
+#include "mlir/Dialect/SPIRV/IR/SPIRVAttributes.h"

fabianmcg wrote:

Back when the compilation redesign was happening we decided to add the attach* 
passes in GPU to avoid polluting lower level dialects with GPU includes. 
However, I do agree that include shouldn't be there, as far as I could tell, 
that include is only needed by one pass option 
`mlir::spirv::TargetEnvAttr::kUnknownDeviceID`, so it should be possible to 
remove it. I'll fix it.

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


[llvm] [clang] [llvm][frontend][offloading] Move clang-linker-wrapper/OffloadWrapper.* to llvm/Frontend/Offloading (PR #78057)

2024-01-13 Thread Fabian Mora via cfe-commits

https://github.com/fabianmcg created 
https://github.com/llvm/llvm-project/pull/78057

This patch moves `clang/tools/clang-linker-wrapper/OffloadWrapper.*` to
`llvm/Frontend/Offloading` allowing them to be reutilized by other projects.

Additionally, it makes minor modifications to the API to make it more flexible.
Concretely:
 - The `wrap*` methods are moved to the `OffloadWrapper` class.
 - The `OffloadWrapper` includes `Suffix` and `EmitSurfacesAndTextures` fields
to specify some additional options.
 - The `Suffix` field is used when emitting the descriptor, registration 
methods,
etc, to make them more readable. It is empty by default.
 - The `EmitSurfacesAndTextures` field controls whether to emit surface and
texture registration code, as those functions were removed from `CUDART`
in CUDA 12. It is true by default.
 - The `wrap*` methods now have an optional field to specify the `EntryArray`;
this change is needed to enable JIT compilation, as ORC doesn't fully support
`__start_` and `__stop_` symbols. Thus, to JIT the code, the `EntryArray` has
to be constructed explicitly in the IR.
 - The function `getOffloadingEntryInitializer` was added to help create the
`EntryArray`, as it returns the constant initializer and not a global variable.

>From f56a7395b19ff634b3ac963204348db2575fdf87 Mon Sep 17 00:00:00 2001
From: Fabian Mora 
Date: Sat, 13 Jan 2024 17:31:51 +
Subject: [PATCH 1/2] Move OffloadWrapper.* to llvm/Frontend/Offloading

---
 .../include/llvm/Frontend/Offloading}/OffloadWrapper.h| 0
 .../lib/Frontend/Offloading}/OffloadWrapper.cpp   | 0
 2 files changed, 0 insertions(+), 0 deletions(-)
 rename {clang/tools/clang-linker-wrapper => 
llvm/include/llvm/Frontend/Offloading}/OffloadWrapper.h (100%)
 rename {clang/tools/clang-linker-wrapper => 
llvm/lib/Frontend/Offloading}/OffloadWrapper.cpp (100%)

diff --git a/clang/tools/clang-linker-wrapper/OffloadWrapper.h 
b/llvm/include/llvm/Frontend/Offloading/OffloadWrapper.h
similarity index 100%
rename from clang/tools/clang-linker-wrapper/OffloadWrapper.h
rename to llvm/include/llvm/Frontend/Offloading/OffloadWrapper.h
diff --git a/clang/tools/clang-linker-wrapper/OffloadWrapper.cpp 
b/llvm/lib/Frontend/Offloading/OffloadWrapper.cpp
similarity index 100%
rename from clang/tools/clang-linker-wrapper/OffloadWrapper.cpp
rename to llvm/lib/Frontend/Offloading/OffloadWrapper.cpp

>From 5c3bdacadbb467176546ab1e6594378dfcc20bc9 Mon Sep 17 00:00:00 2001
From: Fabian Mora 
Date: Sat, 13 Jan 2024 17:43:40 +
Subject: [PATCH 2/2] [llvm][frontend][offloading] Move
 clang-linker-wrapper/OffloadWrapper.* to llvm/Frontend/Offloading

This patch moves `clang/tools/clang-linker-wrapper/OffloadWrapper.*` to
`llvm/Frontend/Offloading` allowing them to be reutilized by other projects.

Additionally, it makes minor modifications to the API to make it more flexible.
Concretely:
 - The `wrap*` methods are moved to the `OffloadWrapper` class.
 - The `OffloadWrapper` includes `Suffix` and `EmitSurfacesAndTextures` fields
to specify some additional options.
 - The `Suffix` field is used when emitting the descriptor, registration 
methods,
etc, to make them more readable. It is empty by default.
 - The `EmitSurfacesAndTextures` field controls whether to emit surface and
texture registration code, as those functions were removed from `CUDART`
in CUDA 12. It is true by default.
 - The `wrap*` methods now have an optional field to specify the `EntryArray`;
this change is needed to enable JIT compilation, as ORC doesn't fully support
`__start_` and `__stop_` symbols. Thus, to JIT the code, the `EntryArray` has
to be constructed explicitly in the IR.
 - The function `getOffloadingEntryInitializer` was added to help create the
`EntryArray`, as it returns the constant initializer and not a global variable.
---
 .../tools/clang-linker-wrapper/CMakeLists.txt |   1 -
 .../ClangLinkerWrapper.cpp|  11 +-
 .../llvm/Frontend/Offloading/OffloadWrapper.h |  60 +++--
 .../llvm/Frontend/Offloading/Utility.h|   6 +
 llvm/lib/Frontend/Offloading/CMakeLists.txt   |   2 +
 .../Frontend/Offloading/OffloadWrapper.cpp| 122 +++---
 llvm/lib/Frontend/Offloading/Utility.cpp  |  21 ++-
 7 files changed, 155 insertions(+), 68 deletions(-)

diff --git a/clang/tools/clang-linker-wrapper/CMakeLists.txt 
b/clang/tools/clang-linker-wrapper/CMakeLists.txt
index 744026a37b22c0..5556869affaa62 100644
--- a/clang/tools/clang-linker-wrapper/CMakeLists.txt
+++ b/clang/tools/clang-linker-wrapper/CMakeLists.txt
@@ -28,7 +28,6 @@ endif()
 
 add_clang_tool(clang-linker-wrapper
   ClangLinkerWrapper.cpp
-  OffloadWrapper.cpp
 
   DEPENDS
   ${tablegen_deps}
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 122ba1998eb83f..ebe8b634c7ae73 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinke

[llvm] [clang] [llvm][frontend][offloading] Move clang-linker-wrapper/OffloadWrapper.* to llvm/Frontend/Offloading (PR #78057)

2024-01-13 Thread Fabian Mora via cfe-commits

https://github.com/fabianmcg updated 
https://github.com/llvm/llvm-project/pull/78057

>From f56a7395b19ff634b3ac963204348db2575fdf87 Mon Sep 17 00:00:00 2001
From: Fabian Mora 
Date: Sat, 13 Jan 2024 17:31:51 +
Subject: [PATCH 1/2] Move OffloadWrapper.* to llvm/Frontend/Offloading

---
 .../include/llvm/Frontend/Offloading}/OffloadWrapper.h| 0
 .../lib/Frontend/Offloading}/OffloadWrapper.cpp   | 0
 2 files changed, 0 insertions(+), 0 deletions(-)
 rename {clang/tools/clang-linker-wrapper => 
llvm/include/llvm/Frontend/Offloading}/OffloadWrapper.h (100%)
 rename {clang/tools/clang-linker-wrapper => 
llvm/lib/Frontend/Offloading}/OffloadWrapper.cpp (100%)

diff --git a/clang/tools/clang-linker-wrapper/OffloadWrapper.h 
b/llvm/include/llvm/Frontend/Offloading/OffloadWrapper.h
similarity index 100%
rename from clang/tools/clang-linker-wrapper/OffloadWrapper.h
rename to llvm/include/llvm/Frontend/Offloading/OffloadWrapper.h
diff --git a/clang/tools/clang-linker-wrapper/OffloadWrapper.cpp 
b/llvm/lib/Frontend/Offloading/OffloadWrapper.cpp
similarity index 100%
rename from clang/tools/clang-linker-wrapper/OffloadWrapper.cpp
rename to llvm/lib/Frontend/Offloading/OffloadWrapper.cpp

>From fad7a36c34bfed207c35cbd0e8e431a6910da792 Mon Sep 17 00:00:00 2001
From: Fabian Mora 
Date: Sat, 13 Jan 2024 17:43:40 +
Subject: [PATCH 2/2] [llvm][frontend][offloading] Move
 clang-linker-wrapper/OffloadWrapper.* to llvm/Frontend/Offloading

This patch moves `clang/tools/clang-linker-wrapper/OffloadWrapper.*` to
`llvm/Frontend/Offloading` allowing them to be reutilized by other projects.

Additionally, it makes minor modifications to the API to make it more flexible.
Concretely:
 - The `wrap*` methods are moved to the `OffloadWrapper` class.
 - The `OffloadWrapper` includes `Suffix` and `EmitSurfacesAndTextures` fields
to specify some additional options.
 - The `Suffix` field is used when emitting the descriptor, registration 
methods,
etc, to make them more readable. It is empty by default.
 - The `EmitSurfacesAndTextures` field controls whether to emit surface and
texture registration code, as those functions were removed from `CUDART`
in CUDA 12. It is true by default.
 - The `wrap*` methods now have an optional field to specify the `EntryArray`;
this change is needed to enable JIT compilation, as ORC doesn't fully support
`__start_` and `__stop_` symbols. Thus, to JIT the code, the `EntryArray` has
to be constructed explicitly in the IR.
 - The function `getOffloadingEntryInitializer` was added to help create the
`EntryArray`, as it returns the constant initializer and not a global variable.
---
 .../tools/clang-linker-wrapper/CMakeLists.txt |   1 -
 .../ClangLinkerWrapper.cpp|  11 +-
 .../llvm/Frontend/Offloading/OffloadWrapper.h |  60 +++--
 .../llvm/Frontend/Offloading/Utility.h|   6 +
 llvm/lib/Frontend/Offloading/CMakeLists.txt   |   2 +
 .../Frontend/Offloading/OffloadWrapper.cpp| 123 +++---
 llvm/lib/Frontend/Offloading/Utility.cpp  |  21 ++-
 7 files changed, 156 insertions(+), 68 deletions(-)

diff --git a/clang/tools/clang-linker-wrapper/CMakeLists.txt 
b/clang/tools/clang-linker-wrapper/CMakeLists.txt
index 744026a37b22c0..5556869affaa62 100644
--- a/clang/tools/clang-linker-wrapper/CMakeLists.txt
+++ b/clang/tools/clang-linker-wrapper/CMakeLists.txt
@@ -28,7 +28,6 @@ endif()
 
 add_clang_tool(clang-linker-wrapper
   ClangLinkerWrapper.cpp
-  OffloadWrapper.cpp
 
   DEPENDS
   ${tablegen_deps}
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 122ba1998eb83f..ebe8b634c7ae73 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -14,11 +14,11 @@
 //
 //===-===//
 
-#include "OffloadWrapper.h"
 #include "clang/Basic/Version.h"
 #include "llvm/BinaryFormat/Magic.h"
 #include "llvm/Bitcode/BitcodeWriter.h"
 #include "llvm/CodeGen/CommandFlags.h"
+#include "llvm/Frontend/Offloading/OffloadWrapper.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/DiagnosticPrinter.h"
 #include "llvm/IR/Module.h"
@@ -906,15 +906,18 @@ wrapDeviceImages(ArrayRef> 
Buffers,
 
   switch (Kind) {
   case OFK_OpenMP:
-if (Error Err = wrapOpenMPBinaries(M, BuffersToWrap))
+if (Error Err =
+offloading::OffloadWrapper().wrapOpenMPBinaries(M, BuffersToWrap))
   return std::move(Err);
 break;
   case OFK_Cuda:
-if (Error Err = wrapCudaBinary(M, BuffersToWrap.front()))
+if (Error Err = offloading::OffloadWrapper().wrapCudaBinary(
+M, BuffersToWrap.front()))
   return std::move(Err);
 break;
   case OFK_HIP:
-if (Error Err = wrapHIPBinary(M, BuffersToWrap.front()))
+if (Error Err = offloading::OffloadWrapper().wrapHIPBinary(
+M, BuffersToWrap.front()))
  

[clang] [llvm] [llvm][frontend][offloading] Move clang-linker-wrapper/OffloadWrapper.* to llvm/Frontend/Offloading (PR #78057)

2024-01-13 Thread Fabian Mora via cfe-commits

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


[clang] [llvm] [llvm][frontend][offloading] Move clang-linker-wrapper/OffloadWrapper.* to llvm/Frontend/Offloading (PR #78057)

2024-01-14 Thread Fabian Mora via cfe-commits

https://github.com/fabianmcg updated 
https://github.com/llvm/llvm-project/pull/78057

>From f56a7395b19ff634b3ac963204348db2575fdf87 Mon Sep 17 00:00:00 2001
From: Fabian Mora 
Date: Sat, 13 Jan 2024 17:31:51 +
Subject: [PATCH 1/3] Move OffloadWrapper.* to llvm/Frontend/Offloading

---
 .../include/llvm/Frontend/Offloading}/OffloadWrapper.h| 0
 .../lib/Frontend/Offloading}/OffloadWrapper.cpp   | 0
 2 files changed, 0 insertions(+), 0 deletions(-)
 rename {clang/tools/clang-linker-wrapper => 
llvm/include/llvm/Frontend/Offloading}/OffloadWrapper.h (100%)
 rename {clang/tools/clang-linker-wrapper => 
llvm/lib/Frontend/Offloading}/OffloadWrapper.cpp (100%)

diff --git a/clang/tools/clang-linker-wrapper/OffloadWrapper.h 
b/llvm/include/llvm/Frontend/Offloading/OffloadWrapper.h
similarity index 100%
rename from clang/tools/clang-linker-wrapper/OffloadWrapper.h
rename to llvm/include/llvm/Frontend/Offloading/OffloadWrapper.h
diff --git a/clang/tools/clang-linker-wrapper/OffloadWrapper.cpp 
b/llvm/lib/Frontend/Offloading/OffloadWrapper.cpp
similarity index 100%
rename from clang/tools/clang-linker-wrapper/OffloadWrapper.cpp
rename to llvm/lib/Frontend/Offloading/OffloadWrapper.cpp

>From fad7a36c34bfed207c35cbd0e8e431a6910da792 Mon Sep 17 00:00:00 2001
From: Fabian Mora 
Date: Sat, 13 Jan 2024 17:43:40 +
Subject: [PATCH 2/3] [llvm][frontend][offloading] Move
 clang-linker-wrapper/OffloadWrapper.* to llvm/Frontend/Offloading

This patch moves `clang/tools/clang-linker-wrapper/OffloadWrapper.*` to
`llvm/Frontend/Offloading` allowing them to be reutilized by other projects.

Additionally, it makes minor modifications to the API to make it more flexible.
Concretely:
 - The `wrap*` methods are moved to the `OffloadWrapper` class.
 - The `OffloadWrapper` includes `Suffix` and `EmitSurfacesAndTextures` fields
to specify some additional options.
 - The `Suffix` field is used when emitting the descriptor, registration 
methods,
etc, to make them more readable. It is empty by default.
 - The `EmitSurfacesAndTextures` field controls whether to emit surface and
texture registration code, as those functions were removed from `CUDART`
in CUDA 12. It is true by default.
 - The `wrap*` methods now have an optional field to specify the `EntryArray`;
this change is needed to enable JIT compilation, as ORC doesn't fully support
`__start_` and `__stop_` symbols. Thus, to JIT the code, the `EntryArray` has
to be constructed explicitly in the IR.
 - The function `getOffloadingEntryInitializer` was added to help create the
`EntryArray`, as it returns the constant initializer and not a global variable.
---
 .../tools/clang-linker-wrapper/CMakeLists.txt |   1 -
 .../ClangLinkerWrapper.cpp|  11 +-
 .../llvm/Frontend/Offloading/OffloadWrapper.h |  60 +++--
 .../llvm/Frontend/Offloading/Utility.h|   6 +
 llvm/lib/Frontend/Offloading/CMakeLists.txt   |   2 +
 .../Frontend/Offloading/OffloadWrapper.cpp| 123 +++---
 llvm/lib/Frontend/Offloading/Utility.cpp  |  21 ++-
 7 files changed, 156 insertions(+), 68 deletions(-)

diff --git a/clang/tools/clang-linker-wrapper/CMakeLists.txt 
b/clang/tools/clang-linker-wrapper/CMakeLists.txt
index 744026a37b22c0..5556869affaa62 100644
--- a/clang/tools/clang-linker-wrapper/CMakeLists.txt
+++ b/clang/tools/clang-linker-wrapper/CMakeLists.txt
@@ -28,7 +28,6 @@ endif()
 
 add_clang_tool(clang-linker-wrapper
   ClangLinkerWrapper.cpp
-  OffloadWrapper.cpp
 
   DEPENDS
   ${tablegen_deps}
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 122ba1998eb83f..ebe8b634c7ae73 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -14,11 +14,11 @@
 //
 //===-===//
 
-#include "OffloadWrapper.h"
 #include "clang/Basic/Version.h"
 #include "llvm/BinaryFormat/Magic.h"
 #include "llvm/Bitcode/BitcodeWriter.h"
 #include "llvm/CodeGen/CommandFlags.h"
+#include "llvm/Frontend/Offloading/OffloadWrapper.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/DiagnosticPrinter.h"
 #include "llvm/IR/Module.h"
@@ -906,15 +906,18 @@ wrapDeviceImages(ArrayRef> 
Buffers,
 
   switch (Kind) {
   case OFK_OpenMP:
-if (Error Err = wrapOpenMPBinaries(M, BuffersToWrap))
+if (Error Err =
+offloading::OffloadWrapper().wrapOpenMPBinaries(M, BuffersToWrap))
   return std::move(Err);
 break;
   case OFK_Cuda:
-if (Error Err = wrapCudaBinary(M, BuffersToWrap.front()))
+if (Error Err = offloading::OffloadWrapper().wrapCudaBinary(
+M, BuffersToWrap.front()))
   return std::move(Err);
 break;
   case OFK_HIP:
-if (Error Err = wrapHIPBinary(M, BuffersToWrap.front()))
+if (Error Err = offloading::OffloadWrapper().wrapHIPBinary(
+M, BuffersToWrap.front()))
  

[clang] [llvm] [llvm][frontend][offloading] Move clang-linker-wrapper/OffloadWrapper.* to llvm/Frontend/Offloading (PR #78057)

2024-01-14 Thread Fabian Mora via cfe-commits


@@ -568,32 +590,45 @@ void createRegisterFatbinFunction(Module &M, 
GlobalVariable *FatbinDesc,
 
 } // namespace
 
-Error wrapOpenMPBinaries(Module &M, ArrayRef> Images) {
-  GlobalVariable *Desc = createBinDesc(M, Images);
+Error OffloadWrapper::wrapOpenMPBinaries(
+Module &M, ArrayRef> Images,
+std::optional EntryArray) const {
+  GlobalVariable *Desc = createBinDesc(
+  M, Images,
+  EntryArray
+  ? *EntryArray
+  : offloading::getOffloadEntryArray(M, "omp_offloading_entries"),
+  Suffix);
   if (!Desc)
 return createStringError(inconvertibleErrorCode(),
  "No binary descriptors created.");
-  createRegisterFunction(M, Desc);
-  createUnregisterFunction(M, Desc);
+  createRegisterFunction(M, Desc, Suffix);

fabianmcg wrote:

So, in MLIR we can have multiple binaries, PTX, fatbinaries in a single IR 
module:
```
gpu.binary @binary_sm_70 [#gpu.object<#nvvm.target, "BINARY 
BLOB">]
gpu.binary @binary_gfx90a [#gpu.object<#rocdel.target, "BINARY 
BLOB">]
...
// Call `kernel_name` in `binary_sm_70`
 gpu.launch_func @binary_sm_70::kernel_name
// Call `kernel_name` in `binary_gfx90a`
 gpu.launch_func @binary_gfx90a::kernel_name
```
I added the suffix field so that in MLIR we can append the binary identifier to 
the descriptor, registration constructor, etc. This makes the IR more readable.

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


[clang] [llvm] [llvm][frontend][offloading] Move clang-linker-wrapper/OffloadWrapper.* to llvm/Frontend/Offloading (PR #78057)

2024-01-14 Thread Fabian Mora via cfe-commits


@@ -568,32 +590,45 @@ void createRegisterFatbinFunction(Module &M, 
GlobalVariable *FatbinDesc,
 
 } // namespace
 
-Error wrapOpenMPBinaries(Module &M, ArrayRef> Images) {
-  GlobalVariable *Desc = createBinDesc(M, Images);
+Error OffloadWrapper::wrapOpenMPBinaries(
+Module &M, ArrayRef> Images,
+std::optional EntryArray) const {
+  GlobalVariable *Desc = createBinDesc(
+  M, Images,
+  EntryArray
+  ? *EntryArray
+  : offloading::getOffloadEntryArray(M, "omp_offloading_entries"),

fabianmcg wrote:

I made it default so `clang-linker-wrapper` didn't see any functional changes, 
while allowing new usages. I think we should revisit this API for project 
offload.

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


[clang] [llvm] [llvm][frontend][offloading] Move clang-linker-wrapper/OffloadWrapper.* to llvm/Frontend/Offloading (PR #78057)

2024-01-14 Thread Fabian Mora via cfe-commits

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


[clang] [llvm] [llvm][frontend][offloading] Move clang-linker-wrapper/OffloadWrapper.* to llvm/Frontend/Offloading (PR #78057)

2024-01-14 Thread Fabian Mora via cfe-commits


@@ -568,32 +590,45 @@ void createRegisterFatbinFunction(Module &M, 
GlobalVariable *FatbinDesc,
 
 } // namespace
 
-Error wrapOpenMPBinaries(Module &M, ArrayRef> Images) {
-  GlobalVariable *Desc = createBinDesc(M, Images);
+Error OffloadWrapper::wrapOpenMPBinaries(
+Module &M, ArrayRef> Images,
+std::optional EntryArray) const {
+  GlobalVariable *Desc = createBinDesc(
+  M, Images,
+  EntryArray
+  ? *EntryArray
+  : offloading::getOffloadEntryArray(M, "omp_offloading_entries"),

fabianmcg wrote:

I see what you mean, first some broader context, this patch is also part of a 
patch series that will add GPU compilation for OMP operations in MLIR without 
the need for `flang` or `clang`, which is not currently possible. This series 
also enables to JIT OMP operations in MLIR. The goal of the series is to make 
OMP target functional in MLIR as a standalone.

I allow the passage of a custom entry array because ORC JIT doesn't fully 
support `__start`, `__stop` symbols for grouping section data.  My solution was 
allowing the custom entry array, so in MLIR I build the full entry array and 
never rely on sections, this applies to OMP, CUDA and HIP.
Thus we have that the following MLIR:
```
module attributes {gpu.container_module} {
  gpu.binary @binary <#gpu.offload_embedding> [#gpu.object<#nvvm.target, 
bin = "BLOB">]
  llvm.func @func() {
%1 = llvm.mlir.constant(1 : index) : i64
gpu.launch_func  @binary::@hello blocks in (%1, %1, %1) threads in (%1, %1, 
%1) : i64
gpu.launch_func  @binary::@world blocks in (%1, %1, %1) threads in (%1, %1, 
%1) : i64
llvm.return
  }
}
```
Produces:
```
@__begin_offload_binary = internal constant [2 x %struct.__tgt_offload_entry] 
[%struct.__tgt_offload_entry { ptr @binary_Khello, ptr 
@.omp_offloading.entry_name, i64 0, i32 0, i32 0 }, %struct.__tgt_offload_entry 
{ ptr @binary_Kworld, ptr @.omp_offloading.entry_name.2, i64 0, i32 0, i32 0 }]
@__end_offload_binary = internal constant ptr getelementptr inbounds 
(%struct.__tgt_offload_entry, ptr @__begin_offload_binary, i64 2)
@.fatbin_image.binary = internal constant [4 x i8] c"BLOB", section ".nv_fatbin"
@.fatbin_wrapper.binary = internal constant %fatbin_wrapper { i32 1180844977, 
i32 1, ptr @.fatbin_image.binary, ptr null }, section ".nvFatBinSegment", align 
8
@.cuda.binary_handle.binary = internal global ptr null
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr 
} { i32 1, ptr @.cuda.fatbin_reg.binary, ptr null }]
@binary_Khello = weak constant i8 0
@.omp_offloading.entry_name = internal unnamed_addr constant [6 x i8] 
c"hello\00"
@binary_Kworld = weak constant i8 0
@.omp_offloading.entry_name.2 = internal unnamed_addr constant [6 x i8] 
c"world\00"
...
```
And this works.

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


[clang] [llvm] [mlir] [mlir][gpu] Add the `OffloadEmbeddingAttr` offloading translation attr (PR #78117)

2024-01-14 Thread Fabian Mora via cfe-commits

https://github.com/fabianmcg created 
https://github.com/llvm/llvm-project/pull/78117

This patch adds the offloading translation attribute. This attribute uses LLVM
offloading infrastructure to embed GPU binaries in the IR. At the program start,
the LLVM offloading mechanism registers kernels and variables with the runtime
library: CUDA RT, HIP RT, or LibOMPTarget.

The offloading mechanism relies on the runtime library to dispatch the correct
kernel based on the registered symbols.

This patch is 3/4 on introducing the `OffloadEmbeddingAttr` GPU translation
attribute.

Note: Ignore the base commits; those are being reviewed in PRs #78057, #78098,
and #78073.


>From 61c8809698b66cf3b4686e9908fb11773ecf0eb6 Mon Sep 17 00:00:00 2001
From: Fabian Mora 
Date: Sat, 13 Jan 2024 23:45:57 +
Subject: [PATCH 1/4] [mlir][interfaces] Add the `TargetInfo` attribute
 interface

This patch adds the TargetInfo attribute interface to the set of DLTI
interfaces. Target information attributes provide essential information on the
compilation target. This information includes the target triple identifier, the
target chip identifier, and a string representation of the target features.

This patch also adds this new interface to the NVVM and ROCDL GPU target
attributes.
---
 .../include/mlir/Dialect/LLVMIR/NVVMDialect.h |  1 +
 mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td   |  5 ++-
 .../mlir/Dialect/LLVMIR/ROCDLDialect.h|  1 +
 mlir/include/mlir/Dialect/LLVMIR/ROCDLOps.td  |  6 ++--
 .../mlir/Interfaces/DataLayoutInterfaces.td   | 33 +++
 mlir/lib/Dialect/LLVMIR/CMakeLists.txt|  2 ++
 mlir/lib/Dialect/LLVMIR/IR/NVVMDialect.cpp|  8 +
 mlir/lib/Dialect/LLVMIR/IR/ROCDLDialect.cpp   |  8 +
 8 files changed, 61 insertions(+), 3 deletions(-)

diff --git a/mlir/include/mlir/Dialect/LLVMIR/NVVMDialect.h 
b/mlir/include/mlir/Dialect/LLVMIR/NVVMDialect.h
index 08019e77ae6af8..1a55d08be9edc2 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/NVVMDialect.h
+++ b/mlir/include/mlir/Dialect/LLVMIR/NVVMDialect.h
@@ -19,6 +19,7 @@
 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
 #include "mlir/IR/Dialect.h"
 #include "mlir/IR/OpDefinition.h"
+#include "mlir/Interfaces/DataLayoutInterfaces.h"
 #include "mlir/Interfaces/SideEffectInterfaces.h"
 #include "llvm/IR/IntrinsicsNVPTX.h"
 
diff --git a/mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td 
b/mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td
index c5f68a2ebe3952..0bbbde6270cd69 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td
@@ -17,6 +17,7 @@ include "mlir/IR/EnumAttr.td"
 include "mlir/Dialect/GPU/IR/CompilationAttrInterfaces.td"
 include "mlir/Dialect/LLVMIR/LLVMOpBase.td"
 include "mlir/Interfaces/SideEffectInterfaces.td"
+include "mlir/Interfaces/DataLayoutInterfaces.td"
 include "mlir/Dialect/LLVMIR/BasicPtxBuilderInterface.td"
 
 def LLVM_PointerGlobal : LLVM_PointerInAddressSpace<1>;
@@ -1894,7 +1895,9 @@ def NVVM_WgmmaMmaAsyncOp : NVVM_Op<"wgmma.mma_async",
 // NVVM target attribute.
 
//===--===//
 
-def NVVM_TargettAttr : NVVM_Attr<"NVVMTarget", "target"> {
+def NVVM_TargettAttr : NVVM_Attr<"NVVMTarget", "target", [
+DeclareAttrInterfaceMethods
+  ]> {
   let description = [{
 GPU target attribute for controlling compilation of NVIDIA targets. All
 parameters decay into default values if not present.
diff --git a/mlir/include/mlir/Dialect/LLVMIR/ROCDLDialect.h 
b/mlir/include/mlir/Dialect/LLVMIR/ROCDLDialect.h
index c2a82ffc1c43cf..fa1131a463e1ab 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/ROCDLDialect.h
+++ b/mlir/include/mlir/Dialect/LLVMIR/ROCDLDialect.h
@@ -26,6 +26,7 @@
 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
 #include "mlir/IR/Dialect.h"
 #include "mlir/IR/OpDefinition.h"
+#include "mlir/Interfaces/DataLayoutInterfaces.h"
 #include "mlir/Interfaces/SideEffectInterfaces.h"
 
 / Ops /
diff --git a/mlir/include/mlir/Dialect/LLVMIR/ROCDLOps.td 
b/mlir/include/mlir/Dialect/LLVMIR/ROCDLOps.td
index 48b830ae34f292..a492709c299544 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/ROCDLOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/ROCDLOps.td
@@ -15,6 +15,7 @@
 
 include "mlir/Dialect/GPU/IR/CompilationAttrInterfaces.td"
 include "mlir/Dialect/LLVMIR/LLVMOpBase.td"
+include "mlir/Interfaces/DataLayoutInterfaces.td"
 include "mlir/Interfaces/SideEffectInterfaces.td"
 
 
//===--===//
@@ -608,8 +609,9 @@ def ROCDL_CvtSrFp8F32Op :
 // ROCDL target attribute.
 
//===--===//
 
-def ROCDL_TargettAttr :
-ROCDL_Attr<"ROCDLTarget", "target"> {
+def ROCDL_TargettAttr : ROCDL_Attr<"ROCDLTarget", "target", [
+DeclareAttrInterfaceMethods
+  ]> {
   let description = [{
 ROCDL target attribute for controlling compilation of AMDGPU targets. All
 parameters decay in

[llvm] [clang] [llvm][frontend][offloading] Move clang-linker-wrapper/OffloadWrapper.* to llvm/Frontend/Offloading (PR #78057)

2024-01-15 Thread Fabian Mora via cfe-commits

https://github.com/fabianmcg updated 
https://github.com/llvm/llvm-project/pull/78057

>From f56a7395b19ff634b3ac963204348db2575fdf87 Mon Sep 17 00:00:00 2001
From: Fabian Mora 
Date: Sat, 13 Jan 2024 17:31:51 +
Subject: [PATCH 1/4] Move OffloadWrapper.* to llvm/Frontend/Offloading

---
 .../include/llvm/Frontend/Offloading}/OffloadWrapper.h| 0
 .../lib/Frontend/Offloading}/OffloadWrapper.cpp   | 0
 2 files changed, 0 insertions(+), 0 deletions(-)
 rename {clang/tools/clang-linker-wrapper => 
llvm/include/llvm/Frontend/Offloading}/OffloadWrapper.h (100%)
 rename {clang/tools/clang-linker-wrapper => 
llvm/lib/Frontend/Offloading}/OffloadWrapper.cpp (100%)

diff --git a/clang/tools/clang-linker-wrapper/OffloadWrapper.h 
b/llvm/include/llvm/Frontend/Offloading/OffloadWrapper.h
similarity index 100%
rename from clang/tools/clang-linker-wrapper/OffloadWrapper.h
rename to llvm/include/llvm/Frontend/Offloading/OffloadWrapper.h
diff --git a/clang/tools/clang-linker-wrapper/OffloadWrapper.cpp 
b/llvm/lib/Frontend/Offloading/OffloadWrapper.cpp
similarity index 100%
rename from clang/tools/clang-linker-wrapper/OffloadWrapper.cpp
rename to llvm/lib/Frontend/Offloading/OffloadWrapper.cpp

>From fad7a36c34bfed207c35cbd0e8e431a6910da792 Mon Sep 17 00:00:00 2001
From: Fabian Mora 
Date: Sat, 13 Jan 2024 17:43:40 +
Subject: [PATCH 2/4] [llvm][frontend][offloading] Move
 clang-linker-wrapper/OffloadWrapper.* to llvm/Frontend/Offloading

This patch moves `clang/tools/clang-linker-wrapper/OffloadWrapper.*` to
`llvm/Frontend/Offloading` allowing them to be reutilized by other projects.

Additionally, it makes minor modifications to the API to make it more flexible.
Concretely:
 - The `wrap*` methods are moved to the `OffloadWrapper` class.
 - The `OffloadWrapper` includes `Suffix` and `EmitSurfacesAndTextures` fields
to specify some additional options.
 - The `Suffix` field is used when emitting the descriptor, registration 
methods,
etc, to make them more readable. It is empty by default.
 - The `EmitSurfacesAndTextures` field controls whether to emit surface and
texture registration code, as those functions were removed from `CUDART`
in CUDA 12. It is true by default.
 - The `wrap*` methods now have an optional field to specify the `EntryArray`;
this change is needed to enable JIT compilation, as ORC doesn't fully support
`__start_` and `__stop_` symbols. Thus, to JIT the code, the `EntryArray` has
to be constructed explicitly in the IR.
 - The function `getOffloadingEntryInitializer` was added to help create the
`EntryArray`, as it returns the constant initializer and not a global variable.
---
 .../tools/clang-linker-wrapper/CMakeLists.txt |   1 -
 .../ClangLinkerWrapper.cpp|  11 +-
 .../llvm/Frontend/Offloading/OffloadWrapper.h |  60 +++--
 .../llvm/Frontend/Offloading/Utility.h|   6 +
 llvm/lib/Frontend/Offloading/CMakeLists.txt   |   2 +
 .../Frontend/Offloading/OffloadWrapper.cpp| 123 +++---
 llvm/lib/Frontend/Offloading/Utility.cpp  |  21 ++-
 7 files changed, 156 insertions(+), 68 deletions(-)

diff --git a/clang/tools/clang-linker-wrapper/CMakeLists.txt 
b/clang/tools/clang-linker-wrapper/CMakeLists.txt
index 744026a37b22c01..5556869affaa62e 100644
--- a/clang/tools/clang-linker-wrapper/CMakeLists.txt
+++ b/clang/tools/clang-linker-wrapper/CMakeLists.txt
@@ -28,7 +28,6 @@ endif()
 
 add_clang_tool(clang-linker-wrapper
   ClangLinkerWrapper.cpp
-  OffloadWrapper.cpp
 
   DEPENDS
   ${tablegen_deps}
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 122ba1998eb83f6..ebe8b634c7ae73f 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -14,11 +14,11 @@
 //
 //===-===//
 
-#include "OffloadWrapper.h"
 #include "clang/Basic/Version.h"
 #include "llvm/BinaryFormat/Magic.h"
 #include "llvm/Bitcode/BitcodeWriter.h"
 #include "llvm/CodeGen/CommandFlags.h"
+#include "llvm/Frontend/Offloading/OffloadWrapper.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/DiagnosticPrinter.h"
 #include "llvm/IR/Module.h"
@@ -906,15 +906,18 @@ wrapDeviceImages(ArrayRef> 
Buffers,
 
   switch (Kind) {
   case OFK_OpenMP:
-if (Error Err = wrapOpenMPBinaries(M, BuffersToWrap))
+if (Error Err =
+offloading::OffloadWrapper().wrapOpenMPBinaries(M, BuffersToWrap))
   return std::move(Err);
 break;
   case OFK_Cuda:
-if (Error Err = wrapCudaBinary(M, BuffersToWrap.front()))
+if (Error Err = offloading::OffloadWrapper().wrapCudaBinary(
+M, BuffersToWrap.front()))
   return std::move(Err);
 break;
   case OFK_HIP:
-if (Error Err = wrapHIPBinary(M, BuffersToWrap.front()))
+if (Error Err = offloading::OffloadWrapper().wrapHIPBinary(
+M, BuffersToWrap.front()))
  

[clang] [llvm] [llvm][frontend][offloading] Move clang-linker-wrapper/OffloadWrapper.* to llvm/Frontend/Offloading (PR #78057)

2024-01-15 Thread Fabian Mora via cfe-commits

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


[clang-tools-extra] [MLIR] SPIRV Target Attribute (PR #69949)

2023-10-26 Thread Fabian Mora via cfe-commits

https://github.com/fabianmcg approved this pull request.


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


[clang] [MLIR] Enabling Intel GPU Integration. (PR #65539)

2023-09-07 Thread Fabian Mora via cfe-commits


@@ -0,0 +1,70 @@
+//===- SerializeToSPIRV.cpp - Convert GPU kernel to SPIRV blob 
-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+///
+/// \file
+/// This pass iterates all the SPIR-V modules in the top module and serializes
+/// each SPIR-V module to SPIR-V binary and then attachs the binary blob as a
+/// string attribute to the corresponding gpu module.
+///
+//===--===//
+
+#include "mlir/Dialect/GPU/Transforms/Passes.h"
+
+#include "mlir/Dialect/GPU/IR/GPUDialect.h"
+#include "mlir/Dialect/GPU/Transforms/Passes.h"
+#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
+#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
+#include "mlir/Target/SPIRV/Serialization.h"
+
+namespace mlir {
+#define GEN_PASS_DEF_GPUSERIALIZETOSPIRVPASS
+#include "mlir/Dialect/GPU/Transforms/Passes.h.inc"
+} // namespace mlir
+
+using namespace mlir;
+
+struct GpuSerializeToSPIRVPass : public 
mlir::impl::GpuSerializeToSPIRVPassBase {
+public:
+  void runOnOperation() override {
+auto mod = getOperation();
+llvm::SmallVector spvBinary;
+for (mlir::gpu::GPUModuleOp gpuMod : mod.getOps()) {
+  auto name = gpuMod.getName();
+  // check that the spv module has the same name with gpu module except the
+  // prefix "__spv__"
+  auto isSameMod = [&](spirv::ModuleOp spvMod) -> bool {
+auto spvModName = spvMod.getName();
+return spvModName->consume_front("__spv__") && spvModName == name;
+  };
+  auto spvMods = mod.getOps();
+  auto it = llvm::find_if(spvMods, isSameMod);
+  if (it == spvMods.end()) {
+gpuMod.emitError() << "Unable to find corresponding SPIR-V module";
+signalPassFailure();
+return;
+  }
+  auto spvMod = *it;
+
+  spvBinary.clear();
+  // serialize the spv module to spv binary
+  if (mlir::failed(spirv::serialize(spvMod, spvBinary))) {
+spvMod.emitError() << "Failed to serialize SPIR-V module";
+signalPassFailure();
+return;
+  }
+
+  // attach the spv binary to the gpu module
+  auto spvData =
+  llvm::StringRef(reinterpret_cast(spvBinary.data()),
+  spvBinary.size() * sizeof(uint32_t));
+  auto spvAttr = mlir::StringAttr::get(&getContext(), spvData);
+  gpuMod->setAttr(gpu::getDefaultGpuBinaryAnnotation(), spvAttr);
+  spvMod->erase();
+}
+  }
+};

fabianmcg wrote:

@silee2 here are the steps:
1. Implement a target attribute, see for example: 
[NVVMTargetAttr](https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td#L1679-L1741).
 The idea of this attribute is to hold properties intrinsic to the target, like 
triple, chip, flags, etc.
2.  Add a pass to attach the target to a module, see: 
[GpuNVVMAttachTarget](https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/Dialect/GPU/Transforms/Passes.td#L85-L128)
 and 
[Dialect/GPU/Transforms/NVVMAttachTarget.cpp](https://github.com/llvm/llvm-project/blob/main/mlir/lib/Dialect/GPU/Transforms/NVVMAttachTarget.cpp).
 The idea of this pass is to attach the SPIRV target to GPU modules, so it must 
know how to create them.
3. We're currently implementing `TargetAttrs` as external models to keep 
libraries separated, see 
[NVVM/Target.cpp](https://github.com/llvm/llvm-project/blob/main/mlir/lib/Target/LLVM/NVVM/Target.cpp#L44-L50),
 so `GpuSerializeToSPIRVPass::run` would be there.
4. Modify `getModuleLoadFn` & `createKernelLaunch` appropriately in 
[SelectObjectAttr.cpp#L125-L15](https://github.com/llvm/llvm-project/blob/main/mlir/lib/Target/LLVMIR/Dialect/GPU/SelectObjectAttr.cpp#L125-L152)
 instead of adding the changes in `GPUToLLVMCommon`.
5. Then the compilation workflow should look something similar to this: [GPU: 
Compilation 
Overview](https://mlir.llvm.org/docs/Dialects/GPU/#compilation-overview)

I'll take care of adding a pointer to the top module symbol table so it can be 
used be the `SPIRVTarget`.

If you have any questions just ping me in discord or discourse `@fabianmc`.

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


[clang-tools-extra] [MLIR] Enabling Intel GPU Integration. (PR #65539)

2023-09-07 Thread Fabian Mora via cfe-commits


@@ -0,0 +1,70 @@
+//===- SerializeToSPIRV.cpp - Convert GPU kernel to SPIRV blob 
-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+///
+/// \file
+/// This pass iterates all the SPIR-V modules in the top module and serializes
+/// each SPIR-V module to SPIR-V binary and then attachs the binary blob as a
+/// string attribute to the corresponding gpu module.
+///
+//===--===//
+
+#include "mlir/Dialect/GPU/Transforms/Passes.h"
+
+#include "mlir/Dialect/GPU/IR/GPUDialect.h"
+#include "mlir/Dialect/GPU/Transforms/Passes.h"
+#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
+#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
+#include "mlir/Target/SPIRV/Serialization.h"
+
+namespace mlir {
+#define GEN_PASS_DEF_GPUSERIALIZETOSPIRVPASS
+#include "mlir/Dialect/GPU/Transforms/Passes.h.inc"
+} // namespace mlir
+
+using namespace mlir;
+
+struct GpuSerializeToSPIRVPass : public 
mlir::impl::GpuSerializeToSPIRVPassBase {
+public:
+  void runOnOperation() override {
+auto mod = getOperation();
+llvm::SmallVector spvBinary;
+for (mlir::gpu::GPUModuleOp gpuMod : mod.getOps()) {
+  auto name = gpuMod.getName();
+  // check that the spv module has the same name with gpu module except the
+  // prefix "__spv__"
+  auto isSameMod = [&](spirv::ModuleOp spvMod) -> bool {
+auto spvModName = spvMod.getName();
+return spvModName->consume_front("__spv__") && spvModName == name;
+  };
+  auto spvMods = mod.getOps();
+  auto it = llvm::find_if(spvMods, isSameMod);
+  if (it == spvMods.end()) {
+gpuMod.emitError() << "Unable to find corresponding SPIR-V module";
+signalPassFailure();
+return;
+  }
+  auto spvMod = *it;
+
+  spvBinary.clear();
+  // serialize the spv module to spv binary
+  if (mlir::failed(spirv::serialize(spvMod, spvBinary))) {
+spvMod.emitError() << "Failed to serialize SPIR-V module";
+signalPassFailure();
+return;
+  }
+
+  // attach the spv binary to the gpu module
+  auto spvData =
+  llvm::StringRef(reinterpret_cast(spvBinary.data()),
+  spvBinary.size() * sizeof(uint32_t));
+  auto spvAttr = mlir::StringAttr::get(&getContext(), spvData);
+  gpuMod->setAttr(gpu::getDefaultGpuBinaryAnnotation(), spvAttr);
+  spvMod->erase();
+}
+  }
+};

fabianmcg wrote:

@silee2 here are the steps:
1. Implement a target attribute, see for example: 
[NVVMTargetAttr](https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td#L1679-L1741).
 The idea of this attribute is to hold properties intrinsic to the target, like 
triple, chip, flags, etc.
2.  Add a pass to attach the target to a module, see: 
[GpuNVVMAttachTarget](https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/Dialect/GPU/Transforms/Passes.td#L85-L128)
 and 
[Dialect/GPU/Transforms/NVVMAttachTarget.cpp](https://github.com/llvm/llvm-project/blob/main/mlir/lib/Dialect/GPU/Transforms/NVVMAttachTarget.cpp).
 The idea of this pass is to attach the SPIRV target to GPU modules, so it must 
know how to create them.
3. We're currently implementing `TargetAttrs` as external models to keep 
libraries separated, see 
[NVVM/Target.cpp](https://github.com/llvm/llvm-project/blob/main/mlir/lib/Target/LLVM/NVVM/Target.cpp#L44-L50),
 so `GpuSerializeToSPIRVPass::run` would be there.
4. Modify `getModuleLoadFn` & `createKernelLaunch` appropriately in 
[SelectObjectAttr.cpp#L125-L15](https://github.com/llvm/llvm-project/blob/main/mlir/lib/Target/LLVMIR/Dialect/GPU/SelectObjectAttr.cpp#L125-L152)
 instead of adding the changes in `GPUToLLVMCommon`.
5. Then the compilation workflow should look something similar to this: [GPU: 
Compilation 
Overview](https://mlir.llvm.org/docs/Dialects/GPU/#compilation-overview)

I'll take care of adding a pointer to the top module symbol table so it can be 
used be the `SPIRVTarget`.

If you have any questions just ping me in discord or discourse `@fabianmc`.

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