Skip to content

Commit

Permalink
generate ir builder for dialect
Browse files Browse the repository at this point in the history
  • Loading branch information
drblallo committed Nov 21, 2024
1 parent 3e9040d commit 2c158a9
Show file tree
Hide file tree
Showing 8 changed files with 404 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ target_link_libraries(dialect PUBLIC rlc::utils MLIRSupport MLIRDialect MLIRLLVM
set(tblgen ${LLVM_BINARY_DIR}/bin/mlir-tblgen)

macro(add_rlc_dialect dialectName)
# Custom Builder
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/include/rlc/${dialectName}/Builder.inc DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/Operations.td
COMMAND rlc-tblgen --print-builder-decls -I ${LLVM_INCLUDE_DIRS} -I ${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/src/Operations.td -o ${CMAKE_CURRENT_BINARY_DIR}/include/rlc/${dialectName}/Builder.inc)
add_custom_target(${dialectName}BuilderInc DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/include/rlc/${dialectName}/Builder.inc rlc-tblgen)

#Dialect
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/src/Dialect.inc DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/Dialect.td
COMMAND ${tblgen} --gen-dialect-defs -I ${LLVM_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}/src/Dialect.td -o ${CMAKE_CURRENT_BINARY_DIR}/src/Dialect.inc)
Expand Down Expand Up @@ -74,7 +79,7 @@ add_custom_target(${dialectName}EnumIncludeInc DEPENDS ${CMAKE_CURRENT_BINARY_DI
add_custom_target(${dialectName}EnumInc DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/src/Enums.inc)

#####
add_dependencies(${dialectName} ${dialectName}PassesIncludeInc ${dialectName}DialectInc ${dialectName}DialectIncludeInc ${dialectName}TypesInc ${dialectName}TypesIncludeInc ${dialectName}OperationsInc ${dialectName}OperationsIncludeInc ${dialectName}TypeInterfacesInc ${dialectName}TypeInterfacesIncludeInc ${dialectName}InterfacesInc ${dialectName}InterfacesIncludeInc ${dialectName}EnumIncludeInc ${dialectName}EnumInc ${dialectName}AttrsIncludeInc ${dialectName}AttrsInc )
add_dependencies(${dialectName} ${dialectName}PassesIncludeInc ${dialectName}DialectInc ${dialectName}DialectIncludeInc ${dialectName}TypesInc ${dialectName}TypesIncludeInc ${dialectName}OperationsInc ${dialectName}OperationsIncludeInc ${dialectName}TypeInterfacesInc ${dialectName}TypeInterfacesIncludeInc ${dialectName}InterfacesInc ${dialectName}InterfacesIncludeInc ${dialectName}EnumIncludeInc ${dialectName}EnumInc ${dialectName}AttrsIncludeInc ${dialectName}AttrsInc ${dialectName}BuilderInc)

endmacro(add_rlc_dialect)

Expand Down
33 changes: 33 additions & 0 deletions lib/dialect/include/rlc/dialect/IRBuilder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once
/*
Copyright 2024 Massimo Fioravanti
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "mlir/IR/Builders.h"
#include "rlc/dialect/Operations.hpp"

namespace mlir::rlc
{
template<typename Base>
class IRBuilderWrapper: public Base
{
public:
using Base::Base;
#include "rlc/dialect/Builder.inc"
};

using IRBuilder = IRBuilderWrapper<mlir::OpBuilder>;

} // namespace mlir::rlc
6 changes: 6 additions & 0 deletions lib/dialect/src/TypeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ limitations under the License.

#include "mlir/IR/BuiltinDialect.h"
#include "mlir/Pass/Pass.h"
#include "rlc/dialect/IRBuilder.hpp"
#include "rlc/dialect/Operations.hpp"
#include "rlc/dialect/conversion/TypeConverter.h"

static void f(mlir::ModuleOp op, mlir::Value lhs, mlir::Value rhs)
{
mlir::rlc::IRBuilder builder(op);
}

static mlir::LogicalResult findClassDecls(
mlir::ModuleOp op, llvm::StringMap<mlir::Operation*>& out)
{
Expand Down
3 changes: 2 additions & 1 deletion lib/parser/include/rlc/parser/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
#include "llvm/Support/Error.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Diagnostics.h"
#include "rlc/dialect/IRBuilder.hpp"
#include "rlc/dialect/Operations.hpp"
#include "rlc/parser/Lexer.hpp"

Expand Down Expand Up @@ -131,7 +132,7 @@ namespace rlc
private:
mlir::Type unkType();
mlir::MLIRContext* ctx;
mlir::OpBuilder builder;
mlir::rlc::IRBuilder builder;
void next();
bool accept(Token t);
template<Token T>
Expand Down
4 changes: 2 additions & 2 deletions lib/parser/src/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ llvm::Expected<mlir::Value> Parser::builtinFromArray()
TRY(size, expression());
EXPECT(Token::RPar);

return builder.create<mlir::rlc::FromByteArrayOp>(
return builder.createFromByteArrayOp(
location, shugarType->getType(), *size, *shugarType);
}

Expand All @@ -121,7 +121,7 @@ llvm::Expected<mlir::Value> Parser::stringExpression()
return value;

location = getCurrentSourcePos();
auto ref = builder.create<mlir::rlc::UnresolvedReference>(location, lIdent);
auto ref = builder.createUnresolvedReference(location, lIdent);

location = getCurrentSourcePos();
return builder
Expand Down
1 change: 1 addition & 0 deletions tool/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ add_subdirectory(rlc-lsp)
add_subdirectory(rlc-fuzz)
add_subdirectory(rlc-doc)
add_subdirectory(rlc-mlir-lsp)
add_subdirectory(rlc-tblgen)
9 changes: 9 additions & 0 deletions tool/rlc-tblgen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
get_property(translation_libs GLOBAL PROPERTY MLIR_TRANSLATION_LIBS)
rlcAddTool(rlc-tblgen
MLIRTblgenLib
LLVMTableGen
MLIRTableGen
${dialect_libs}
${llvm_libs}
${translation_libs})
target_include_directories(rlc-tblgen PUBLIC ${MLIR_INCLUDE_DIRS})
Loading

0 comments on commit 2c158a9

Please sign in to comment.