Skip to content

Initial GPU pipeline #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions cmake/functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,17 @@ endfunction()

function(gc_add_mlir_dialect_library name)
add_mlir_dialect_library(${ARGV})
target_link_libraries(obj.${name} PUBLIC GcInterface)
set_property(GLOBAL APPEND PROPERTY GC_DIALECT_LIBS ${name})

if(GcInterface IN_LIST ARGN)
target_link_libraries(obj.${name} PUBLIC GcInterface)
endif()
endfunction()
endfunction()

function(gc_add_mlir_translation_library name)
add_mlir_translation_library(${ARGV})
set_property(GLOBAL APPEND PROPERTY GC_MLIR_LIBS ${name})
if(GcInterface IN_LIST ARGN)
target_link_libraries(obj.${name} PUBLIC GcInterface)
endif()
endfunction()
1 change: 1 addition & 0 deletions include/gc/Dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ add_subdirectory(CPURuntime)
add_subdirectory(OneDNNGraph)
add_subdirectory(Microkernel)
add_subdirectory(Linalgx)
add_subdirectory(LLVMIR)
6 changes: 6 additions & 0 deletions include/gc/Dialect/LLVMIR/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_mlir_dialect(GenOps gen)
add_mlir_doc(GenOps GENDialect Dialects/ -gen-dialect-doc -dialect=gen)
set(LLVM_TARGET_DEFINITIONS GenOps.td)
mlir_tablegen(GenOpsAttributes.h.inc -gen-attrdef-decls -attrdefs-dialect=gen)
mlir_tablegen(GenOpsAttributes.cpp.inc -gen-attrdef-defs -attrdefs-dialect=gen)
add_public_tablegen_target(MLIRGENConversionsIncGen)
22 changes: 22 additions & 0 deletions include/gc/Dialect/LLVMIR/GENDialect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- GENDialect.h - MLIR GEN target definitions --------------*- C++ -*-===//
//
// This file is licensed 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
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_DIALECT_LLVMIR_GENDIALECT_H_
#define MLIR_DIALECT_LLVMIR_GENDIALECT_H_

#include "mlir/Bytecode/BytecodeOpInterface.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/OpDefinition.h"

#define GET_ATTRDEF_CLASSES
#include "gc/Dialect/LLVMIR/GenOpsAttributes.h.inc"

#include "gc/Dialect/LLVMIR/GenOpsDialect.h.inc"

#endif /* MLIR_DIALECT_LLVMIR_XEDEFS_H_ */
75 changes: 75 additions & 0 deletions include/gc/Dialect/LLVMIR/GenOps.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//===-- GenOps.td - Gen dialect definition -----------------*- tablegen -*-===//
//
// This file is licensed 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
//
//===----------------------------------------------------------------------===//
#ifndef GENIR_OPS
#define GENIR_OPS

include "mlir/Dialect/GPU/IR/CompilationAttrInterfaces.td"
include "mlir/Dialect/LLVMIR/LLVMOpBase.td"
include "mlir/Dialect/SPIRV/IR/SPIRVBase.td"
include "mlir/Interfaces/SideEffectInterfaces.td"

def GEN_Dialect : Dialect {
let name = "gen";
let cppNamespace = "::mlir::gen";
let dependentDialects = ["LLVM::LLVMDialect"];
let hasOperationAttrVerify = 1;

let extraClassDeclaration = [{
/// Get the name of the attribute used to annotate external kernel
/// functions.
static StringRef getKernelFuncAttrName() { return "gen.kernel"; }
/// The address space value that represents global memory.
static constexpr unsigned kGlobalMemoryAddressSpace = 1;
/// The address space value that represents shared memory.
static constexpr unsigned kSharedMemoryAddressSpace = 3;
/// The address space value that represents private memory.
static constexpr unsigned kPrivateMemoryAddressSpace = 0;
}];

let useDefaultAttributePrinterParser = 1;
}

class GEN_Attr<string attrName, string attrMnemonic, list<Trait> traits = []>
: AttrDef<GEN_Dialect, attrName, traits> {
let mnemonic = attrMnemonic;
}

def GEN_TargettAttr : GEN_Attr<"GenTarget", "target"> {
let description = [{
GPU target attribute for controlling compilation of targets. All
parameters decay into default values if not present.

Examples:

1. Target with default values.
```
gpu.module @mymodule [#gen.target] attributes {...} {
...
}
```
}];
let parameters = (ins
DefaultValuedParameter<"int", "2", "Optimization level to apply.">:$O,
StringRefParameter<"Target triple.", "\"spirv64-unknown-unknown\"">:$triple,
StringRefParameter<"Target chip.", "\"pvc\"">:$chip
);
let assemblyFormat = [{
(`<` struct($O, $triple, $chip)^ `>`)?
}];
let builders = [
AttrBuilder<(ins CArg<"int", "2">:$optLevel,
CArg<"StringRef", "\"spirv64-unknown-unknown\"">:$triple,
CArg<"StringRef", "\"pvc\"">:$chip), [{
return Base::get($_ctxt, optLevel, triple, chip);
}]>
];
let skipDefaultBuilders = 1;
let genVerifyDecl = 1;
}

#endif // GENIR_OPS
30 changes: 30 additions & 0 deletions include/gc/Target/LLVM/GEN/Target.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===-- Target.h - MLIR GEN target registration -----------------*- C++ -*-===//
//
// This file is licensed 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 provides registration calls for attaching the Gen target interface.
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_TARGET_GEN_TARGET_H
#define MLIR_TARGET_GEN_TARGET_H

namespace mlir {
class DialectRegistry;
class MLIRContext;
namespace gen {
/// Registers the `TargetAttrInterface` for the `#gen.target` attribute in
/// the given registry.
void registerGenTargetInterfaceExternalModels(DialectRegistry &registry);

/// Registers the `TargetAttrInterface` for the `#gen.target` attribute in
/// the registry associated with the given context.
void registerGenTargetInterfaceExternalModels(MLIRContext &context);
} // namespace gen
} // namespace mlir

#endif // MLIR_TARGET_GEN_TARGET_H
53 changes: 53 additions & 0 deletions include/gc/Target/LLVM/GEN/Utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//===-- Utils.h - MLIR GEN target utils -------------------------*- C++ -*-===//
//
// This file is licensed 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 files declares GEN target related utility classes and functions.
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_TARGET_LLVM_GEN_UTILS_H
#define MLIR_TARGET_LLVM_GEN_UTILS_H

#include "gc/Dialect/LLVMIR/GENDialect.h"
#include "mlir/Dialect/GPU/IR/CompilationInterfaces.h"
#include "mlir/Target/LLVM/ModuleToObject.h"

namespace mlir {
namespace gen {

StringRef getONEAPIToolkitPath();

/// Base class for all GEN serializations from GPU modules into binary strings.
/// By default this class serializes into LLVM bitcode.
class SerializeGPUModuleBase : public LLVM::ModuleToObject {
public:
/// Initializes the `toolkitPath` with the path in `targetOptions` or if empty
/// with the path in `getONEAPIToolkitPath`.
SerializeGPUModuleBase(Operation &module, GenTargetAttr target,
const gpu::TargetOptions &targetOptions = {});

// Initialize intermediate spirv target llvm backend
static void init();

/// Returns the target attribute.
GenTargetAttr getTarget() const;

/// Returns the ONEAPI toolkit path.
StringRef getToolkitPath() const;

protected:
/// GEN target attribute.
GenTargetAttr target;

/// ONEAPI toolkit path.
std::string toolkitPath;
};
} // namespace gen
} // namespace mlir

#endif // MLIR_TARGET_LLVM_GEN_UTILS_H
31 changes: 31 additions & 0 deletions include/gc/Target/LLVMIR/Dialect/GEN/GENToLLVMIRTranslation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===-- GENToLLVMIRTranslation.h - GEN to LLVM IR ---------------*- C++ -*-===//
//
// This file is licensed 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 provides registration calls for GEN dialect to LLVM IR translation.
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_TARGET_LLVMIR_DIALECT_GEN_GENTOLLVMIRTRANSLATION_H
#define MLIR_TARGET_LLVMIR_DIALECT_GEN_GENTOLLVMIRTRANSLATION_H

namespace mlir {

class DialectRegistry;
class MLIRContext;

/// Register the GEN dialect and the translation from it to the LLVM IR in the
/// given registry;
void registerGENDialectTranslation(DialectRegistry &registry);

/// Register the GEN dialect and the translation from it in the registry
/// associated with the given context.
void registerGENDialectTranslation(MLIRContext &context);

} // namespace mlir

#endif // MLIR_TARGET_LLVMIR_DIALECT_GEN_GENTOLLVMIRTRANSLATION_H
19 changes: 19 additions & 0 deletions include/gc/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,23 @@ def LinalgToXeGPU : Pass<"linalg-to-xegpu", "func::FuncOp"> {
}
#endif

def GpuLegalizeModule: Pass<"gc-gpu-legalize-module", ""> {
let summary = "Legalizes a GPU module for spirv conversion.";
let description = [{
Update all the nested gpu modules with an appropriate spirv target
information that is used further down in the pipeline.
}];
let dependentDialects = ["gpu::GPUDialect", "spirv::SPIRVDialect"];
}

def ConvertGpuSignaturesToLLVM: Pass<"gc-gpu-signatures-to-llvm", "gpu::GPUModuleOp"> {
let summary = "Legalize GPU kernel signatures for runtime code conversion.";
let dependentDialects = ["gpu::GPUDialect", "memref::MemRefDialect"];
}

def GpuGenAttachTarget: Pass<"gc-attach-gen-target", ""> {
let summary = "Attaches Gen target to a GPU module.";
let dependentDialects = ["gpu::GPUDialect", "gen::GENDialect"];
}

#endif // GC_DIALECT_GC_PASSES
2 changes: 2 additions & 0 deletions lib/gc/CAPI/Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ using namespace mlir::cpuruntime;

namespace mlir::gc {
void registerCPUPipeline();
void registerGPUPipeline();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
void registerGPUPipeline();
#ifdef GC_USE_GPU
void registerGPUPipeline();
#endif

} // namespace mlir::gc

#ifdef __cplusplus
Expand All @@ -29,6 +30,7 @@ extern "C" {

MLIR_CAPI_EXPORTED void mlirRegisterAllGCPassesAndPipelines() {
registerCPUPipeline();
registerGPUPipeline();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
registerGPUPipeline();
#ifdef GC_USE_GPU
registerGPUPipeline();
#endif

mlirRegisterCPURuntimePasses();
mlirRegisterGraphCompilerPasses();
}
Expand Down
1 change: 1 addition & 0 deletions lib/gc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ add_subdirectory(CAPI)
add_subdirectory(Dialect)
add_subdirectory(Transforms)
add_subdirectory(ExecutionEngine)
add_subdirectory(Target)
1 change: 1 addition & 0 deletions lib/gc/Dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ add_subdirectory(CPURuntime)
add_subdirectory(Linalgx)
add_subdirectory(Microkernel)
add_subdirectory(OneDNNGraph)
add_subdirectory(LLVMIR)
20 changes: 20 additions & 0 deletions lib/gc/Dialect/LLVMIR/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
gc_add_mlir_dialect_library(MLIRGENDialect
IR/GENDialect.cpp

ADDITIONAL_HEADER_DIRS
${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/LLVMIR
${PROJECT_SOURCE_DIR}/include/gc/Dialect/LLVMIR

DEPENDS
MLIRGENConversionsIncGen

LINK_COMPONENTS
AsmParser
Core

LINK_LIBS PUBLIC
MLIRIR
MLIRLLVMDialect
MLIRSideEffectInterfaces
GcInterface
)
57 changes: 57 additions & 0 deletions lib/gc/Dialect/LLVMIR/IR/GENDialect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//===-- GENDialect.cpp - GEN Attrs and dialect registration -----*- C++ -*-===//
//
// This file is licensed 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
//
//===----------------------------------------------------------------------===//
#include "gc/Dialect/LLVMIR/GENDialect.h"

#include "mlir/Dialect/GPU/IR/CompilationInterfaces.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/IR/DialectImplementation.h"
#include "llvm/ADT/TypeSwitch.h"

using namespace mlir;
using namespace gen;

#include "gc/Dialect/LLVMIR/GenOpsDialect.cpp.inc"

LogicalResult
GenTargetAttr::verify(function_ref<InFlightDiagnostic()> emitError, int O,
StringRef triple, StringRef chip) {
if (O < 0 || O > 3) {
emitError() << "The optimization level must be a number between 0 and 3.";
return failure();
}
if (triple.empty()) {
emitError() << "The target triple cannot be empty.";
return failure();
}
if (chip.empty()) {
emitError() << "The target chip cannot be empty.";
return failure();
}
return success();
}

LogicalResult GENDialect::verifyOperationAttribute(Operation *op,
NamedAttribute attr) {
return success();
}

void GENDialect::initialize() {
// clang-tidy is confused by the registration mechanism
// NOLINTBEGIN
addAttributes<
#define GET_ATTRDEF_LIST
#include "gc/Dialect/LLVMIR/GenOpsAttributes.cpp.inc"
>();
// NOLINTEND

allowUnknownOperations();
declarePromisedInterface<gpu::TargetAttrInterface, GenTargetAttr>();
}

#define GET_ATTRDEF_CLASSES
#include "gc/Dialect/LLVMIR/GenOpsAttributes.cpp.inc"
2 changes: 2 additions & 0 deletions lib/gc/Target/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_subdirectory(LLVMIR)
add_subdirectory(LLVM)
Loading