Skip to content

Add portable SPIR-V and OpenCL GPU backends - #439

Open
aobolensk wants to merge 6 commits into
intel:mainfrom
aobolensk:portable-be
Open

aobolensk wants to merge 6 commits into
intel:mainfrom
aobolensk:portable-be

Conversation

@aobolensk

@aobolensk aobolensk commented Oct 31, 2026 •

Copy link
Copy Markdown

Support execution on other (non-Intel) GPUs

This is a prerequisite for support of non-Intel GPUs in OpenVINO GPU plugin via MLIR

Comment thread CMakeLists.txt Outdated
option(GC_ENABLE_OPT "Build gc-opt" ${GC_ENABLE_TOOLS})
option(GC_ENABLE_ZE "Build Level Zero GPU runtime" ON)
option(GC_ENABLE_ZE "Build Level Zero GPU runtime" ${GC_ENABLE_IGC_DEFAULT})
option(GC_ENABLE_OCL "Build OpenCL source GPU runtime" ON)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be OFF by default.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread include/gc/Runtime/OclSourceModule.h Outdated
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest a different design:

  • Implement a new OclRuntime, that creates/launches kernels, built from source/spirv/binary.
  • Use GpuCompiler.compile() for the module compilation. It compiles and links with the symbols from your runtime.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback!

  • Replaced OclSourceModule with OclRuntime, which creates and launches kernels from OpenCL source, SPIR-V or native binaries
  • Module compilation now goes through GpuCompiler.compile(), which links the generated code against the runtime symbols

Comment thread cmake/mlir.cmake
MLIRUBDialect
MLIRVectorDialect
MLIRVectorTransforms
MLIRSPIRVDialect

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(GC_ENABLE_OCL) ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SPIR-V code generation also in fact works with GC_ENABLE_OCL=OFF, so these dependencies should remain unconditional

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds new libs unconditionally, which increases the binary size. Have you checked the binary size with GC_ENABLE_OCL=OFF ? It should remain the same as before this PR. We have tight requirements for the bin size.

@aobolensk

Copy link
Copy Markdown
Author

@AndreyPavlenko could you please take a look?

@aobolensk

Copy link
Copy Markdown
Author

ping

Comment thread cmake/mlir.cmake
MLIRUBDialect
MLIRVectorDialect
MLIRVectorTransforms
MLIRSPIRVDialect

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds new libs unconditionally, which increases the binary size. Have you checked the binary size with GC_ENABLE_OCL=OFF ? It should remain the same as before this PR. We have tight requirements for the bin size.

else if (name == "arith.remsi") binary("%");
else if (name == "arith.divui" || name == "arith.remui") {
auto type = unsignedTypeName(op.getOperand(0).getType());
expression = "(((" + type + ")" + operands[0] +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A closing parenthesis seems missing here.

check(clEnqueueReadBuffer(impl->queue, source, CL_FALSE, 0, size, dst,
nDeps, deps, &event));
else {
if (nDeps) check(clWaitForEvents(nDeps, deps));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It holds the mutex and locks other threads.


const DeviceInfo &OclRuntime::getDeviceInfo() const { return impl->info; }

OclRuntime::Memory *OclRuntime::importBuffer(cl_mem buffer) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like a memory leak. Do you really need it?

Comment on lines +18 to +19

class OclRuntime final : public GpuRuntime<OclKernel, _cl_event, _cl_mem> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class OclRuntime final : public GpuRuntime<OclKernel, _cl_event, _cl_mem> {
struct OclMemory
class OclRuntime final : public GpuRuntime<OclKernel, _cl_event, OclMemory> {

If the runtime operates on OclMemory instead of _cl_mem, then you can save whatever you need in OclMemory and get rid of importBuffer().

@dchigarev

Copy link
Copy Markdown
Contributor

@aobolensk thank you for your interest in our project, I have a few high-level questions and would appreciate some context on the work you're doing:

  1. What's your end goal? Do I understand correctly that you want to run some models on GPU via OV with non-intel GPUs and GC happens to be the only path you can achieve that with? Which devices and which models you're primarily targeting, have you already tried running some model through this path? Are there any plans to contribute to OV and extend/modify the MLIR-path there? (e.g. changing the converters or something?)

  2. As far as I see, the new spirv/ocl pipelines do not reuse any passes from our xegpu-pipeline, which basically makes it a completely separate pipeline. If that's the case, I would prefer to make our existing populateGPUPipeline a pure dispatcher between different pipelines, something like:

    void populateGPUPipeline(OpPassManager &pm, const GPUPipelineOptions &opts) {
      if (opts.backend == GpuBackend::XeGPU)
        return populateXeGPUPipeline(pm, opts); // our current pipeline
      else if (opts.backend == GpuBackend::SPIRV)
        return populateSPIRVPipeline(pm, opts);
      else if (opts.backend == GpuBackend::OpenCL)
        return populateOpenCLPipeline(pm, opts);
      else
        throw std::exception(...);
    }
    
    // e.g.
    void populateSPIRVPipeline(...) {
    #ifndef GC_ENABLE_SPIRV_PIPELINE
        throw std::exception("not supported ... or something");
    #else
        return _populateSPIRVPipeline(...);
    #endif
    }

    Maybe we can do something similar with getDialectRegistry too? I don't really like how there are currently '#ifdefs` branching all around the function, I would prefer to split them into independent functions is possible. Asking about the future: do you have any plan to reuse 'linalg -> vector' or other parts of the xegpu pipeline? What's your primary goal for now: performance or functional support of a non-intel-gpu in OV?

  3. We've reorganized our folders structure a bit in Reorganize gpu-transforms #482. Could you please put your new passes into lib/gc/Transforms/GPU/Portable?

  4. I'm a bit concerned regarding the 'ocl-backend' path in general. Could you please clarify why do you need it and why spirv path is not enough for your goal? The existing OclSource pass doesn't look very robust and extendable, honestly it looks like a hack that doesn't really belong to an mlir-pipeline/gc in general. Do you have any serious plans to support and extend this path in the future?

  5. Why do you need to add an opencl-runtime to gc? If you want to run models through openvino you have to use openvino's runtime-wrappers (e.g. an implementation of gc::GpuRuntime in ov), if you want opencl-runtime support for OV-models you would have to change things there

  6. cc @AndreyPavlenko do you think it makes sense to split our GcGpuPasses library into several libs like: GcGpuCommonPasses (reusable lib), GcXeGPUPasses (our current pipeline), GcGpuPortablePasses (new code from this PR),(if it's possible at all?)


TLDR: I'm okay with the spirv-path in general (maybe we should name it somehow differently? technically our xegpu path also produces spirv), let's just try to separate it as much as possible from the existing pipeline if there are no plans to reuse parts from the existing one (e.g. avoid if-defs in the common code, try to place the new code into new libraries when possible, to avoid conditional dependencies in the existing targets), but I'm not really sure about the ocl-path

Comment thread include/gc/GpuCompiler.h
Comment on lines +45 to +46
bool spirv{false};
bool source{false};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a single 'enum target' field?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants