Skip to content

Commit 2fed342

Browse files
committed
Simple load store test works
1 parent 6c407ec commit 2fed342

File tree

14 files changed

+355
-37
lines changed

14 files changed

+355
-37
lines changed

include/gc/Conversion/Passes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#define GC_CONVERSION_PASSES_H
1111

1212
#include "gc/Conversion/XeVMToLLVM/XeVMToLLVM.h"
13+
#include "mlir/Pass/Pass.h"
14+
#include "mlir/Pass/PassOptions.h"
1315

1416
namespace mlir {
1517

include/gc/ExecutionEngine/Driver/Driver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace mlir {
1818
class DialectRegistry;
1919
namespace gc {
2020

21-
const DialectRegistry &initCompilerAndGetDialects();
21+
DialectRegistry &initCompilerAndGetDialects();
2222

2323
// the pointers to XXXMemRefType
2424
using GeneralMemrefPtr = void *;

lib/gc/Conversion/XeVMToLLVM/XeVMToLLVM.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "gc/Dialect/LLVMIR/XeVMDialect.h"
1212
#include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h"
1313
#include "mlir/Conversion/LLVMCommon/Pattern.h"
14+
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
1415
#include "mlir/Dialect/LLVMIR/FunctionCallUtils.h"
1516
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
1617
#include "mlir/Pass/Pass.h"
@@ -115,13 +116,13 @@ getCacheControlMetadata(ConversionPatternRewriter &rewriter, OpType op,
115116
return rewriter.getArrayAttr(combinedAttrs);
116117
}
117118

118-
static LLVM::CallOp
119-
createDeviceFunctionCall(ConversionPatternRewriter &rewriter,
120-
StringRef funcName, Type retType,
121-
ArrayRef<Type> argTypes, ArrayRef<Value> args,
122-
ArrayRef<std::pair<unsigned, StringRef>> paramAttrs,
123-
LLVMFuncAttributeOptions funcAttributeOptions) {
124-
auto moduleOp = rewriter.getBlock()->getParent()->getParentOfType<ModuleOp>();
119+
static LLVM::CallOp createDeviceFunctionCall(
120+
ConversionPatternRewriter &rewriter, StringRef funcName, Type retType,
121+
ArrayRef<Type> argTypes, ArrayRef<Value> args,
122+
mlir::ArrayRef<std::pair<unsigned, mlir::StringRef>> paramAttrs,
123+
LLVMFuncAttributeOptions funcAttributeOptions) {
124+
auto moduleOp =
125+
rewriter.getBlock()->getParent()->getParentOfType<gpu::GPUModuleOp>();
125126
MLIRContext *ctx = rewriter.getContext();
126127
Location loc = UnknownLoc::get(ctx);
127128

lib/gc/ExecutionEngine/Driver/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ else()
2525
MLIRToLLVMIRTranslationRegistration
2626
)
2727
endif()
28+
get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS)
2829

2930
set(GC_PASSES GcInterface GcPasses)
3031
if(GC_ENABLE_IMEX)
@@ -38,6 +39,8 @@ gc_add_mlir_library(GcJitWrapper
3839
${MLIR_LINK_COMPONENTS}
3940
${dialect_libs}
4041
${conversion_libs}
42+
${extension_libs}
4143
${GC_PASSES}
4244
GcAnalysis
45+
MLIRXeVMToLLVMIRTranslation
4346
)

lib/gc/ExecutionEngine/Driver/Driver.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
#ifdef GC_HAS_ONEDNN_DIALECT
1212
#include "gc/Dialect/OneDNNGraph/OneDNNGraphDialect.h"
1313
#endif
14+
#include "gc/Conversion/Passes.h"
15+
#include "gc/Target/LLVM/XeVM/Target.h"
16+
#include "gc/Target/LLVMIR/Dialect/XeVM/XeVMToLLVMIRTranslation.h"
1417
#include "gc/Transforms/Passes.h"
1518
#include "mlir/InitAllDialects.h"
19+
#include "mlir/InitAllExtensions.h"
1620
#include "mlir/InitAllPasses.h"
1721
#include "mlir/Pass/PassManager.h"
1822
#include "mlir/Target/LLVMIR/Dialect/All.h"
@@ -26,22 +30,29 @@ namespace gc {
2630
static DialectRegistry initDialects() {
2731
mlir::registerAllPasses();
2832
mlir::gc::registerGraphCompilerPasses();
33+
mlir::registerGCConversionPasses();
2934
mlir::cpuruntime::registerCPURuntimePasses();
3035
mlir::DialectRegistry registry;
3136
registry.insert<mlir::cpuruntime::CPURuntimeDialect>();
3237
mlir::registerAllDialects(registry);
3338
mlir::cpuruntime::registerConvertCPURuntimeToLLVMInterface(registry);
39+
mlir::registerAllExtensions(registry);
40+
// Adds missing `LLVMTranslationDialectInterface` registration for dialect for
41+
// gpu.module op
42+
mlir::registerAllToLLVMIRTranslations(registry);
43+
mlir::registerConvertXeVMToLLVMInterface(registry);
44+
mlir::registerXeVMDialectTranslation(registry);
45+
mlir::xevm::registerXeVMTargetInterfaceExternalModels(registry);
3446
#ifdef GC_HAS_ONEDNN_DIALECT
3547
registry.insert<mlir::onednn_graph::OneDNNGraphDialect>();
3648
#endif
3749
llvm::InitializeNativeTarget();
3850
llvm::InitializeNativeTargetAsmPrinter();
3951
llvm::InitializeNativeTargetAsmParser();
40-
mlir::registerAllToLLVMIRTranslations(registry);
4152
return registry;
4253
}
4354

44-
const DialectRegistry &initCompilerAndGetDialects() {
55+
DialectRegistry &initCompilerAndGetDialects() {
4556
static DialectRegistry reg = initDialects();
4657
return reg;
4758
}

lib/gc/ExecutionEngine/OpenCLRuntime/OpenCLRuntimeWrappers.cpp

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,79 @@
2323
#define OCL_RUNTIME_EXPORT GC_DLL_EXPORT
2424

2525
namespace {
26+
/* clang-format off */
27+
#define CaseToString(x) case x: return #x;
28+
const char* opencl_errstr(cl_int err) {
29+
switch (err){
30+
CaseToString(CL_SUCCESS)
31+
CaseToString(CL_DEVICE_NOT_FOUND)
32+
CaseToString(CL_DEVICE_NOT_AVAILABLE)
33+
CaseToString(CL_COMPILER_NOT_AVAILABLE)
34+
CaseToString(CL_MEM_OBJECT_ALLOCATION_FAILURE)
35+
CaseToString(CL_OUT_OF_RESOURCES)
36+
CaseToString(CL_OUT_OF_HOST_MEMORY)
37+
CaseToString(CL_PROFILING_INFO_NOT_AVAILABLE)
38+
CaseToString(CL_MEM_COPY_OVERLAP)
39+
CaseToString(CL_IMAGE_FORMAT_MISMATCH)
40+
CaseToString(CL_IMAGE_FORMAT_NOT_SUPPORTED)
41+
CaseToString(CL_BUILD_PROGRAM_FAILURE)
42+
CaseToString(CL_MAP_FAILURE)
43+
CaseToString(CL_MISALIGNED_SUB_BUFFER_OFFSET)
44+
CaseToString(CL_COMPILE_PROGRAM_FAILURE)
45+
CaseToString(CL_LINKER_NOT_AVAILABLE)
46+
CaseToString(CL_LINK_PROGRAM_FAILURE)
47+
CaseToString(CL_DEVICE_PARTITION_FAILED)
48+
CaseToString(CL_KERNEL_ARG_INFO_NOT_AVAILABLE)
49+
CaseToString(CL_INVALID_VALUE)
50+
CaseToString(CL_INVALID_DEVICE_TYPE)
51+
CaseToString(CL_INVALID_PLATFORM)
52+
CaseToString(CL_INVALID_DEVICE)
53+
CaseToString(CL_INVALID_CONTEXT)
54+
CaseToString(CL_INVALID_QUEUE_PROPERTIES)
55+
CaseToString(CL_INVALID_COMMAND_QUEUE)
56+
CaseToString(CL_INVALID_HOST_PTR)
57+
CaseToString(CL_INVALID_MEM_OBJECT)
58+
CaseToString(CL_INVALID_IMAGE_FORMAT_DESCRIPTOR)
59+
CaseToString(CL_INVALID_IMAGE_SIZE)
60+
CaseToString(CL_INVALID_SAMPLER)
61+
CaseToString(CL_INVALID_BINARY)
62+
CaseToString(CL_INVALID_BUILD_OPTIONS)
63+
CaseToString(CL_INVALID_PROGRAM)
64+
CaseToString(CL_INVALID_PROGRAM_EXECUTABLE)
65+
CaseToString(CL_INVALID_KERNEL_NAME)
66+
CaseToString(CL_INVALID_KERNEL_DEFINITION)
67+
CaseToString(CL_INVALID_KERNEL)
68+
CaseToString(CL_INVALID_ARG_INDEX)
69+
CaseToString(CL_INVALID_ARG_VALUE)
70+
CaseToString(CL_INVALID_ARG_SIZE)
71+
CaseToString(CL_INVALID_KERNEL_ARGS)
72+
CaseToString(CL_INVALID_WORK_DIMENSION)
73+
CaseToString(CL_INVALID_WORK_GROUP_SIZE)
74+
CaseToString(CL_INVALID_WORK_ITEM_SIZE)
75+
CaseToString(CL_INVALID_GLOBAL_OFFSET)
76+
CaseToString(CL_INVALID_EVENT_WAIT_LIST)
77+
CaseToString(CL_INVALID_EVENT)
78+
CaseToString(CL_INVALID_OPERATION)
79+
CaseToString(CL_INVALID_GL_OBJECT)
80+
CaseToString(CL_INVALID_BUFFER_SIZE)
81+
CaseToString(CL_INVALID_MIP_LEVEL)
82+
CaseToString(CL_INVALID_GLOBAL_WORK_SIZE)
83+
CaseToString(CL_INVALID_PROPERTY)
84+
CaseToString(CL_INVALID_IMAGE_DESCRIPTOR)
85+
CaseToString(CL_INVALID_COMPILER_OPTIONS)
86+
CaseToString(CL_INVALID_LINKER_OPTIONS)
87+
CaseToString(CL_INVALID_DEVICE_PARTITION_COUNT)
88+
default : return "Unknown OpenCL error code";
89+
}
90+
}
91+
/* clang-format on */
2692

2793
#define CL_SAFE_CALL2(a) \
2894
do { \
2995
(a); \
3096
if (err != CL_SUCCESS) { \
31-
fprintf(stderr, "FAIL: err=%d @ line=%d (%s)\n", err, __LINE__, (#a)); \
97+
fprintf(stderr, "FAIL: err=%d (%s) @ line=%d (%s)\n", err, \
98+
opencl_errstr(err), __LINE__, (#a)); \
3299
abort(); \
33100
} \
34101
} while (0)
@@ -37,8 +104,8 @@ namespace {
37104
{ \
38105
auto status = (call); \
39106
if (status != CL_SUCCESS) { \
40-
fprintf(stderr, "CL error %d @ line=%d (%s)\n", status, __LINE__, \
41-
(#call)); \
107+
fprintf(stderr, "CL error %d (%s) @ line=%d (%s)\n", status, \
108+
opencl_errstr(status), __LINE__, (#call)); \
42109
abort(); \
43110
} \
44111
}
@@ -159,7 +226,8 @@ static cl_device_id getDevice(cl_device_type *devtype) {
159226
}
160227

161228
std::vector<cl_device_id> devices(uintValue);
162-
clGetDeviceIDs(platform, *devtype, uintValue, devices.data(), nullptr);
229+
CL_SAFE_CALL(
230+
clGetDeviceIDs(platform, *devtype, uintValue, devices.data(), nullptr));
163231

164232
for (auto &device : devices) {
165233
CL_SAFE_CALL(clGetDeviceInfo(device, CL_DEVICE_VENDOR_ID, sizeof(cl_uint),
@@ -447,29 +515,35 @@ extern "C" OCL_RUNTIME_EXPORT void gpuWait(GPUCLQUEUE *queue) {
447515

448516
// a silly workaround for mgpuModuleLoad. OCL needs context and device to load
449517
// the module. We remember the last call to any mgpu* APIs
450-
static thread_local GPUCLQUEUE *lastQueue;
518+
static thread_local GPUCLQUEUE *lastQueue{nullptr};
451519
extern "C" OCL_RUNTIME_EXPORT GPUCLQUEUE *mgpuStreamCreate() {
452520
auto ret =
453521
new GPUCLQUEUE(static_cast<cl_device_id>(nullptr), nullptr, nullptr);
454522
lastQueue = ret;
455523
return ret;
456524
}
457525

526+
GPUCLQUEUE *getOrCreateStaticQueue() {
527+
if (!lastQueue) {
528+
return mgpuStreamCreate();
529+
}
530+
return lastQueue;
531+
}
532+
458533
extern "C" OCL_RUNTIME_EXPORT void mgpuStreamDestroy(GPUCLQUEUE *queue) {
459534
lastQueue = nullptr;
460535
delete queue;
461536
}
462537

463538
extern "C" OCL_RUNTIME_EXPORT void *
464539
mgpuMemAlloc(uint64_t size, GPUCLQUEUE *queue, bool isShared) {
465-
lastQueue = queue;
466-
return allocDeviceMemory(queue, size, /*alignment*/ 64, isShared);
540+
return allocDeviceMemory(queue ? queue : getOrCreateStaticQueue(), size,
541+
/*alignment*/ 64, isShared);
467542
}
468543

469544
extern "C" OCL_RUNTIME_EXPORT void mgpuMemFree(void *ptr, GPUCLQUEUE *queue) {
470-
lastQueue = queue;
471545
if (ptr) {
472-
deallocDeviceMemory(queue, ptr);
546+
deallocDeviceMemory(queue ? queue : getOrCreateStaticQueue(), ptr);
473547
}
474548
}
475549

@@ -498,8 +572,8 @@ mgpuLaunchKernel(cl_kernel kernel, size_t gridX, size_t gridY, size_t gridZ,
498572
size_t sharedMemBytes, GPUCLQUEUE *queue, void **params,
499573
void ** /*extra*/, size_t paramsCount) {
500574
launchKernel(
501-
queue, kernel, gridX, gridY, gridZ, blockX, blockY, blockZ,
502-
sharedMemBytes,
575+
queue ? queue : getOrCreateStaticQueue(), kernel, gridX, gridY, gridZ,
576+
blockX, blockY, blockZ, sharedMemBytes,
503577
[&]() {
504578
// todo (yijie): do we need to handle shared mem? If there is dynamic
505579
// shared mem required, which value should paramsCount be?
@@ -512,5 +586,5 @@ mgpuLaunchKernel(cl_kernel kernel, size_t gridX, size_t gridY, size_t gridZ,
512586
}
513587

514588
extern "C" OCL_RUNTIME_EXPORT void mgpuStreamSynchronize(GPUCLQUEUE *queue) {
515-
CL_SAFE_CALL(clFinish(queue->queue_));
589+
CL_SAFE_CALL(clFinish((queue ? queue : getOrCreateStaticQueue())->queue_));
516590
}

lib/gc/Target/LLVM/XeVM/Target.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,11 @@ SpirSerializer::moduleToObject(llvm::Module &llvmModule) {
144144
getOperation().emitError() << "Failed translating the module to Binary.";
145145
return std::nullopt;
146146
}
147-
148-
StringRef bin(serializedSPIRVBinary->c_str(),
149-
serializedSPIRVBinary->size() + 1);
147+
if (serializedSPIRVBinary->size() % 4) {
148+
getOperation().emitError() << "SPIRV code size must be a multiple of 4.";
149+
return std::nullopt;
150+
}
151+
StringRef bin(serializedSPIRVBinary->c_str(), serializedSPIRVBinary->size());
150152
return SmallVector<char, 0>(bin.begin(), bin.end());
151153
}
152154

@@ -166,7 +168,7 @@ SpirSerializer::translateToSPIRVBinary(llvm::Module &llvmModule,
166168

167169
codegenPasses.run(llvmModule);
168170
}
169-
return stream.str();
171+
return targetISA;
170172
}
171173

172174
std::optional<SmallVector<char, 0>>

src/gc-cpu-runner/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
################################################################################
2-
# Copyright (C) 2024 Intel Corporation
2+
# Copyright (C) 2025 Intel Corporation
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -32,5 +32,5 @@ if(GC_DEV_LINK_LLVM_DYLIB)
3232
endif()
3333

3434
gc_add_mlir_tool(gc-cpu-runner gc-cpu-runner.cpp)
35-
target_link_libraries(gc-cpu-runner PRIVATE GcCpuRuntime)
35+
target_link_libraries(gc-cpu-runner PRIVATE GcJitWrapper GcCpuRuntime)
3636
mlir_check_all_link_libraries(gc-cpu-runner)

src/gc-cpu-runner/gc-cpu-runner.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
/*
3-
* Copyright (C) 2024 Intel Corporation
3+
* Copyright (C) 2025 Intel Corporation
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
1717
* SPDX-License-Identifier: Apache-2.0
1818
*/
1919

20+
#include "gc/ExecutionEngine/Driver/Driver.h"
21+
2022
#include "mlir/Dialect/Arith/IR/Arith.h"
2123
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
2224
#include "mlir/ExecutionEngine/JitRunner.h"
@@ -34,13 +36,8 @@ int main(int argc, char **argv) {
3436
// keeps GCCPURuntime linked
3537
gc_runtime_keep_alive = 0;
3638
llvm::InitLLVM y(argc, argv);
37-
llvm::InitializeNativeTarget();
38-
llvm::InitializeNativeTargetAsmPrinter();
39-
llvm::InitializeNativeTargetAsmParser();
40-
41-
mlir::DialectRegistry registry;
39+
mlir::DialectRegistry &registry{mlir::gc::initCompilerAndGetDialects()};
4240
registry.insert<mlir::arith::ArithDialect>();
43-
mlir::registerAllToLLVMIRTranslations(registry);
4441

4542
return mlir::JitRunnerMain(argc, argv, registry);
4643
}

src/gc-opt/gc-opt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
/*
3-
* Copyright (C) 2024 Intel Corporation
3+
* Copyright (C) 2025 Intel Corporation
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)