Skip to content

Commit

Permalink
treewide: Introduce core function interface to avoid implicit symbol …
Browse files Browse the repository at this point in the history
…from mlir::FunctionOpInterface.
  • Loading branch information
xlauko committed Oct 8, 2024
1 parent 918890d commit aa38d6b
Show file tree
Hide file tree
Showing 37 changed files with 1,719 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ namespace vast::conv::tc {
maybe_type_t convert_memref_type(mlir::UnrankedMemRefType t) { return {}; }

maybe_signature_conversion_t
get_conversion_signature(mlir::FunctionOpInterface fn, bool variadic) {
get_conversion_signature(core::function_op_interface fn, bool variadic) {
signature_conversion_t conversion(fn.getNumArguments());
auto fn_type = mlir::dyn_cast< core::FunctionType >(fn.getFunctionType());
VAST_ASSERT(fn_type);
Expand Down
2 changes: 2 additions & 0 deletions include/vast/Conversion/TypeConverters/TypeConverter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ VAST_UNRELAX_WARNINGS
#include <gap/core/crtp.hpp>

#include "vast/Dialect/Core/CoreTypes.hpp"
#include "vast/Dialect/Core/Interfaces/FunctionInterface.hpp"

#include "vast/Dialect/HighLevel/HighLevelDialect.hpp"

#include "vast/Util/Common.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace vast::conv::tc {
// TODO(conv:tc): This should probably be some interface instead, since
// we are only updating the root?
logical_result replace(
mlir::FunctionOpInterface fn,
core::function_op_interface fn,
auto &rewriter
) const {
auto old_type = fn.getFunctionType();
Expand Down Expand Up @@ -115,7 +115,7 @@ namespace vast::conv::tc {
operation op, mlir::ArrayRef< mlir::Value >,
conversion_rewriter &rewriter
) const override {
if (auto func_op = mlir::dyn_cast< mlir::FunctionOpInterface >(op))
if (auto func_op = mlir::dyn_cast< core::function_op_interface >(op))
return this->replace(func_op, rewriter);
return this->replace(op, rewriter);
}
Expand Down
2 changes: 2 additions & 0 deletions include/vast/Dialect/ABI/ABIOps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ VAST_RELAX_WARNINGS
#include "vast/Dialect/Core/CoreTypes.hpp"
#include "vast/Dialect/Core/CoreAttributes.hpp"
#include "vast/Dialect/Core/Func.hpp"

#include "vast/Dialect/Core/Interfaces/FunctionInterface.hpp"
#include "vast/Dialect/Core/Interfaces/SymbolInterface.hpp"
#include "vast/Dialect/Core/Interfaces/SymbolTableInterface.hpp"

Expand Down
1 change: 0 additions & 1 deletion include/vast/Dialect/Builtin/Ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "vast/Util/Warnings.hpp"

VAST_RELAX_WARNINGS
#include <mlir/Interfaces/FunctionInterfaces.h>
#include <mlir/Interfaces/CallInterfaces.h>
#include <mlir/Interfaces/InferTypeOpInterface.h>
VAST_UNRELAX_WARNINGS
Expand Down
1 change: 0 additions & 1 deletion include/vast/Dialect/Core/CoreLazy.td
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ include "mlir/IR/OpBase.td"

include "mlir/Interfaces/CallInterfaces.td"
include "mlir/Interfaces/CastInterfaces.td"
include "mlir/Interfaces/FunctionInterfaces.td"
include "vast/Dialect/Core/Interfaces/SymbolInterface.td"

class Core_LazyEval< string mnemonic, list < Trait > traits = [] >
Expand Down
3 changes: 1 addition & 2 deletions include/vast/Dialect/Core/CoreOps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "vast/Util/Common.hpp"
#include "vast/Util/TypeList.hpp"

#include <mlir/Interfaces/FunctionInterfaces.h>
#include <mlir/Interfaces/CallInterfaces.h>
#include <mlir/Interfaces/InferTypeOpInterface.h>
#include <mlir/Interfaces/DataLayoutInterfaces.h>
Expand All @@ -24,4 +23,4 @@

namespace vast::core {
using module = core::ModuleOp;
} // namespace vast::core
} // namespace vast::core
9 changes: 6 additions & 3 deletions include/vast/Dialect/Core/Func.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ VAST_UNRELAX_WARNINGS
#include "vast/Dialect/Core/Linkage.hpp"
#include "vast/Dialect/Core/CoreTypes.hpp"

#include "vast/Dialect/Core/Interfaces/FunctionInterface.hpp"
#include "vast/Dialect/Core/Interfaces/FunctionImplementation.hpp"

namespace vast::core {

llvm::StringRef getLinkageAttrNameString();
Expand Down Expand Up @@ -77,11 +80,11 @@ namespace vast::core {
}

auto fty = op.getFunctionType();
mlir::function_interface_impl::printFunctionSignature(
vast::core::function_interface_impl::printFunctionSignature(
printer, op, fty.getInputs(), fty.isVarArg(), fty.getResults()
);

mlir::function_interface_impl::printFunctionAttributes(
vast::core::function_interface_impl::printFunctionAttributes(
printer, op, { getLinkageAttrNameString(), op.getFunctionTypeAttrName() }
);

Expand Down Expand Up @@ -160,7 +163,7 @@ namespace vast::core {

template< typename DstFuncOp >
logical_result convert_and_replace_function(auto src, auto &rewriter) {
return convert_and_replace_function< DstFuncOp >(src, rewriter, src.getName());
return convert_and_replace_function< DstFuncOp >(src, rewriter, src.getSymbolName());
}

} // namespace vast::core
7 changes: 3 additions & 4 deletions include/vast/Dialect/Core/Func.td
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
include "mlir/IR/OpBase.td"

include "mlir/Interfaces/CallInterfaces.td"
include "mlir/Interfaces/FunctionInterfaces.td"
include "vast/Dialect/Core/Interfaces/SymbolInterface.td"
include "vast/Dialect/Core/Interfaces/FunctionInterface.td"

include "vast/Dialect/Core/CoreTraits.td"
include "vast/Dialect/Core/LinkageHelper.td"
Expand All @@ -22,9 +22,8 @@ class Core_FuncBaseOp< Dialect dialect, string mnemonic, list< Trait > traits =
: Op< dialect, mnemonic,
!listconcat(traits, [
AutomaticAllocationScope,
CallableOpInterface,
FunctionOpInterface,
IsolatedFromAbove,
Core_FunctionOpInterface,
Core_FuncSymbol,
NoTerminator
])
Expand Down Expand Up @@ -86,7 +85,7 @@ class Core_FuncBaseOp< Dialect dialect, string mnemonic, list< Trait > traits =
if (arg_attrs.empty())
return;

mlir::function_interface_impl::addArgAndResultAttrs(
vast::core::function_interface_impl::addArgAndResultAttrs(
$_builder, $_state, arg_attrs, res_attrs,
getArgAttrsAttrName($_state.name), getResAttrsAttrName($_state.name)
);
Expand Down
1 change: 1 addition & 0 deletions include/vast/Dialect/Core/Interfaces/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_vast_op_interface(DeclStorageInterface)
add_vast_op_interface(FunctionInterface)
add_vast_op_interface(TypeDefinitionInterface)

add_vast_op_interface(SymbolInterface)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
#include "vast/Util/Warnings.hpp"

VAST_RELAX_WARNINGS
#include <mlir/Interfaces/FunctionInterfaces.h>
#include <mlir/IR/BuiltinOps.h>
#include <mlir/IR/BuiltinTypes.h>
#include <mlir/IR/Dialect.h>
#include <mlir/IR/OperationSupport.h>
VAST_RELAX_WARNINGS

#include "vast/Dialect/Core/CoreOps.hpp"
#include "vast/Dialect/Core/Interfaces/FunctionInterface.hpp"

#define GET_OP_FWD_DEFINES
#include "vast/Dialect/HighLevel/HighLevel.h.inc"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def Core_DeclStorageInterface : Core_OpInterface< "DeclStorageInterface" > {
return kind_attr.getValue();
}
auto st = core::get_effective_symbol_table_for< core::var_symbol >($_op)->get_defining_operation();
if (mlir::isa< mlir::FunctionOpInterface >(st))
if (mlir::isa< core::function_op_interface >(st))
return DeclContextKind::dc_function;
if (st->template hasTrait< core::ScopeLikeTrait >())
return DeclContextKind::dc_function;
Expand Down
108 changes: 108 additions & 0 deletions include/vast/Dialect/Core/Interfaces/FunctionImplementation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//===- FunctionImplementation.h - Function-like Op utilities ----*- C++ -*-===//
//
// 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 provides utility functions for implementing function-like
// operations, in particular, parsing, printing and verification components
// common to function-like operations.
//
//===----------------------------------------------------------------------===//

#ifndef VAST_DIALECT_CORE_FUNCTIONIMPLEMENTATION_HPP
#define VAST_DIALECT_CORE_FUNCTIONIMPLEMENTATION_HPP

#include "vast/Util/Warnings.hpp"

VAST_RELAX_WARNINGS
#include <mlir/IR/OpImplementation.h>
VAST_UNRELAX_WARNINGS

#include "vast/Dialect/Core/Interfaces/FunctionInterface.hpp"

namespace vast::core {

namespace function_interface_impl {

/// A named class for passing around the variadic flag.
class VariadicFlag {
public:
explicit VariadicFlag(bool variadic) : variadic(variadic) {}
bool isVariadic() const { return variadic; }

private:
/// Underlying storage.
bool variadic;
};

/// Adds argument and result attributes, provided as `argAttrs` and
/// `resultAttrs` arguments, to the list of operation attributes in `result`.
/// Internally, argument and result attributes are stored as dict attributes
/// with special names given by getResultAttrName, getArgumentAttrName.
void addArgAndResultAttrs(::mlir::Builder &builder, ::mlir::OperationState &result,
::mlir::ArrayRef<::mlir::DictionaryAttr> argAttrs,
::mlir::ArrayRef<::mlir::DictionaryAttr> resultAttrs,
::mlir::StringAttr argAttrsName, ::mlir::StringAttr resAttrsName);
void addArgAndResultAttrs(::mlir::Builder &builder, ::mlir::OperationState &result,
::mlir::ArrayRef<::mlir::OpAsmParser::Argument> args,
::mlir::ArrayRef<::mlir::DictionaryAttr> resultAttrs,
::mlir::StringAttr argAttrsName, ::mlir::StringAttr resAttrsName);

/// Callback type for `parseFunctionOp`, the callback should produce the
/// type that will be associated with a function-like operation from lists of
/// function arguments and results, VariadicFlag indicates whether the function
/// should have variadic arguments; in case of error, it may populate the last
/// argument with a message.
using FuncTypeBuilder = ::mlir::function_ref<::mlir::Type(
::mlir::Builder &, ::mlir::ArrayRef<::mlir::Type>, ::mlir::ArrayRef<::mlir::Type>, VariadicFlag, std::string &)>;

/// Parses a function signature using `parser`. The `allowVariadic` argument
/// indicates whether functions with variadic arguments are supported. The
/// trailing arguments are populated by this function with names, types,
/// attributes and locations of the arguments and those of the results.
::mlir::ParseResult
parseFunctionSignature(::mlir::OpAsmParser &parser, bool allowVariadic,
::mlir::SmallVectorImpl<::mlir::OpAsmParser::Argument> &arguments,
bool &isVariadic, ::mlir::SmallVectorImpl<::mlir::Type> &resultTypes,
::mlir::SmallVectorImpl<::mlir::DictionaryAttr> &resultAttrs);

/// Parser implementation for function-like operations. Uses
/// `funcTypeBuilder` to construct the custom function type given lists of
/// input and output types. The parser sets the `typeAttrName` attribute to the
/// resulting function type. If `allowVariadic` is set, the parser will accept
/// trailing ellipsis in the function signature and indicate to the builder
/// whether the function is variadic. If the builder returns a null type,
/// `result` will not contain the `type` attribute. The caller can then add a
/// type, report the error or delegate the reporting to the op's verifier.
::mlir::ParseResult parseFunctionOp(::mlir::OpAsmParser &parser, ::mlir::OperationState &result,
bool allowVariadic, ::mlir::StringAttr typeAttrName,
FuncTypeBuilder funcTypeBuilder,
::mlir::StringAttr argAttrsName, ::mlir::StringAttr resAttrsName);

/// Printer implementation for function-like operations.
void printFunctionOp(::mlir::OpAsmPrinter &p, FunctionOpInterface op, bool isVariadic,
::mlir::StringRef typeAttrName, ::mlir::StringAttr argAttrsName,
::mlir::StringAttr resAttrsName);

/// Prints the signature of the function-like operation `op`. Assumes `op` has
/// is a FunctionOpInterface and has passed verification.
void printFunctionSignature(::mlir::OpAsmPrinter &p, FunctionOpInterface op,
::mlir::ArrayRef<::mlir::Type> argTypes, bool isVariadic,
::mlir::ArrayRef<::mlir::Type> resultTypes);

/// Prints the list of function prefixed with the "attributes" keyword. The
/// attributes with names listed in "elided" as well as those used by the
/// function-like operation internally are not printed. Nothing is printed
/// if all attributes are elided. Assumes `op` is a FunctionOpInterface and
/// has passed verification.
void printFunctionAttributes(::mlir::OpAsmPrinter &p, ::mlir::Operation *op,
::mlir::ArrayRef<::mlir::StringRef> elided = {});

} // namespace function_interface_impl

} // namespace vast::core

#endif // VAST_DIALECT_CORE_FUNCTIONIMPLEMENTATION_HPP
Loading

0 comments on commit aa38d6b

Please sign in to comment.