Skip to content
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

Parser type conversions #750

Merged
merged 24 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
58a1293
conv: Add unrealized conversion materialization helper.
xlauko Nov 19, 2024
b3999ee
core: Add arithmetic binary op interface.
xlauko Nov 20, 2024
2424432
hl: Annotate arithmetic operations with core trait.
xlauko Nov 20, 2024
95561f7
pr: Reconcile unrealized casts on the fly.
xlauko Nov 20, 2024
ad64dcb
pr: Let parser detector invoke standard conversions.
xlauko Nov 20, 2024
8d8a930
pr: Fix MaybeDataType predicate to include NoDataType.
xlauko Nov 20, 2024
d67a714
pr: Fix fclose model to expect nodata.
xlauko Nov 20, 2024
f388241
pr: Swap AnyDataType and MaybeDataType.
xlauko Nov 20, 2024
4e4b55f
pr: Resurrect type constraints.
xlauko Nov 20, 2024
d770373
pr: Add to_maybe cast.
xlauko Nov 20, 2024
5171843
conv: Make function signature conversion allow to reflect on index of…
xlauko Nov 22, 2024
b512ade
conv: Add parser function conversion.
xlauko Nov 22, 2024
d4556e5
pr: Add model for main function.
xlauko Nov 22, 2024
c165958
pr: Add parser decl.
xlauko Nov 22, 2024
06017f6
conv: Add param to parser decl conversion.
xlauko Nov 22, 2024
934fad2
pr: Add reference operation.
xlauko Nov 22, 2024
f42479c
hl: Fix scope annotations.
xlauko Nov 22, 2024
cfe9b0d
conv: Introduce decl ref converion to parser ref.
xlauko Nov 22, 2024
2bda66a
pr: Fix include.
xlauko Nov 22, 2024
df8f2b2
pr: Fix types for decl ref conversion.
xlauko Nov 22, 2024
c41da62
conv: Add return conversion pattern.
xlauko Nov 22, 2024
8f44b39
conv: Add non-parsing cmp conversion.
xlauko Nov 22, 2024
0b52fcd
conv: Simplify unrealized_materialization.
xlauko Nov 25, 2024
6f04dc7
tools: Remove obsolete detect-parsers.
xlauko Nov 25, 2024
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
27 changes: 18 additions & 9 deletions include/vast/Conversion/Parser/default-parsers-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
model:
return_type: nodata
arguments:
- anydata # const char * restrict format
- anydata # ...
- maybedata # const char * restrict format
- maybedata # ...
category: sink

# int fprintf(FILE * restrict stream, const char * restrict format, ...);
Expand All @@ -77,33 +77,33 @@
return_type: nodata
arguments:
- nodata # FILE * restrict stream
- anydata # const char * restrict format
- anydata # ...
- maybedata # const char * restrict format
- maybedata # ...
category: sink

# void perror(const char *s);
- function: perror
model:
return_type: nodata
arguments:
- anydata # const char *s
- maybedata # const char *s
category: sink

# void free(void * ptr);
- function: free
model:
return_type: nodata
arguments:
- anydata # void * ptr
- maybedata # void * ptr
category: sink

# FILE * fopen(const char * restrict filename, const char * restrict mode);
- function: fopen
model:
return_type: nodata
arguments:
- anydata # const char * restrict filename
- anydata # const char * restrict mode
- maybedata # const char * restrict filename
- maybedata # const char * restrict mode
category: sink

#
Expand Down Expand Up @@ -151,5 +151,14 @@
model:
return_type: nodata
arguments:
- anydata # FILE * stream
- nodata # FILE * stream
category: nonparser

- function: main
model:
return_type: nodata
arguments:
- nodata # int argc
- data # char * argv[]
- data # char * envp[]
category: nonparser
25 changes: 23 additions & 2 deletions include/vast/Conversion/TypeConverters/TypeConverter.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022, Trail of Bits, Inc.

Check notice on line 1 in include/vast/Conversion/TypeConverters/TypeConverter.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter (19, 22.04)

Run clang-format on include/vast/Conversion/TypeConverters/TypeConverter.hpp

File include/vast/Conversion/TypeConverters/TypeConverter.hpp does not conform to Custom style guidelines. (lines 49, 50, 56)

#pragma once

Expand Down Expand Up @@ -45,6 +45,19 @@
}
};

static inline auto unrealized_materialization(
mlir::OpBuilder &builder, mlir::Type resultType,
mlir::ValueRange inputs, mlir::Location loc
) -> std::optional< mlir::Value > {
if (inputs.size() != 1) {
return std::nullopt;
}

return builder
.create< mlir::UnrealizedConversionCastOp >(loc, resultType, inputs)
.getResult(0);
};

struct identity_type_converter : base_type_converter
{
using base = base_type_converter;
Expand Down Expand Up @@ -139,6 +152,10 @@
.template take_wrapped< maybe_type_t >();
}

maybe_type_t convert_arg_type(mlir_type t, unsigned long /* idx */) const {
xlauko marked this conversation as resolved.
Show resolved Hide resolved
return convert_type_to_type(t);
}

auto appender(types_t &out) const {
return [&](auto collection) {
out.insert(
Expand All @@ -165,8 +182,12 @@

maybe_signature_conversion_t signature_conversion(const auto &inputs) const {
signature_conversion_t sc(inputs.size());
if (mlir::failed(self().convertSignatureArgs(inputs, sc))) {
return {};
for (auto [i, arg] : llvm::enumerate(inputs)) {
if (auto trg = self().convert_arg_type(arg, i)) {
sc.addInputs(i, *trg);
} else {
return {};
}
}
return { std::move(sc) };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ namespace vast::conv::tc {
return replace_impl(op, rewriter, tc);
}

logical_result replace(operation op, auto &rewriter) const {
auto tc = static_cast< const type_converter & >(*self().getTypeConverter());
return replace(op, rewriter, tc);
}

private:
const auto &self() const { return static_cast< const derived & >(*this); }

logical_result replace_impl(core::function_op_interface fn, auto &rewriter, const type_converter &tc) const {
auto old_type = fn.getFunctionType();
Expand Down Expand Up @@ -81,8 +75,8 @@ namespace vast::conv::tc {
}

void fixup_entry_block(mlir::Block &block, const type_converter &tc) const {
for (auto arg : block.getArguments()) {
auto trg = tc.convert_type_to_type(arg.getType());
for (auto [idx, arg] : llvm::enumerate(block.getArguments())) {
auto trg = tc.convert_arg_type(arg.getType(), idx);
VAST_CHECK(trg, "Type conversion failed: {0}", arg);
arg.setType(*trg);
}
Expand Down Expand Up @@ -114,7 +108,8 @@ namespace vast::conv::tc {
operation op, mlir::ArrayRef< mlir::Value >,
conversion_rewriter &rewriter
) const override {
return replace(op, rewriter);
auto tc = static_cast< const type_converter & >(*getTypeConverter());
return replace(op, rewriter, tc);
}
};

Expand Down
2 changes: 2 additions & 0 deletions include/vast/Dialect/Core/Interfaces/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ add_vast_op_interface(DeclStorageInterface)
add_vast_op_interface(FunctionInterface)
add_vast_op_interface(TypeDefinitionInterface)

add_vast_op_interface(OperationInterfaces)

add_vast_op_interface(SymbolInterface)
add_vast_op_interface(SymbolTableInterface)
add_vast_attr_interface(SymbolRefInterface)
Expand Down
16 changes: 16 additions & 0 deletions include/vast/Dialect/Core/Interfaces/OperationInterfaces.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2024, Trail of Bits, Inc.

Check notice on line 1 in include/vast/Dialect/Core/Interfaces/OperationInterfaces.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter (19, 22.04)

Run clang-format on include/vast/Dialect/Core/Interfaces/OperationInterfaces.hpp

File include/vast/Dialect/Core/Interfaces/OperationInterfaces.hpp does not conform to Custom style guidelines. (lines 8)

#pragma once

#include "vast/Util/Warnings.hpp"

VAST_RELAX_WARNINGS
#include <mlir/IR/BuiltinOps.h>
#include <mlir/IR/BuiltinTypes.h>
#include <mlir/IR/Dialect.h>
#include <mlir/IR/OperationSupport.h>
#include <llvm/ADT/StringRef.h>
VAST_RELAX_WARNINGS

/// Include the generated interface declarations.
#include "vast/Dialect/Core/Interfaces/OperationInterfaces.h.inc"

Check failure on line 16 in include/vast/Dialect/Core/Interfaces/OperationInterfaces.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter (19, 22.04)

include/vast/Dialect/Core/Interfaces/OperationInterfaces.hpp:16:10 [clang-diagnostic-error]

'vast/Dialect/Core/Interfaces/OperationInterfaces.h.inc' file not found
17 changes: 17 additions & 0 deletions include/vast/Dialect/Core/Interfaces/OperationInterfaces.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) 2024, Trail of Bits, Inc.

#ifndef VAST_INTERFACES_OPERATION_INTERFACES
#define VAST_INTERFACES_OPERATION_INTERFACES

include "mlir/IR/OpBase.td"

include "vast/Dialect/Core/CoreTraits.td"
include "vast/Dialect/Core/Interfaces/Common.td"

def Core_ArithBinOp : Core_OpInterface< "ArithBinOpInterface" > {
let description = [{
This interface describes an operation that is arithmetic binary operation.
}];
}

#endif // VAST_INTERFACES_OPERATION_INTERFACES
52 changes: 37 additions & 15 deletions include/vast/Dialect/HighLevel/HighLevelCF.td
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,13 @@ def HighLevel_TypeYieldOp
let assemblyFormat = [{ attr-dict $result `:` type($result) }];
}

def HighLevel_IfOp
: HighLevel_ControlFlowOp< "if" >
{
def HighLevel_IfOp : HighLevel_ControlFlowOp< "if", [
NoTerminator, Core_ScopeLikeTrait,
Core_ShadowingSymbolTable< [
[Core_VarSymbol, Core_TypeSymbol, Core_EnumConstantSymbol],
[Core_ElaboratedTypeSymbol]
] >
] > {
let summary = "VAST if statement";
let description = [{
The operation takes builders of two mandatory regions -- condition and then
Expand Down Expand Up @@ -293,9 +297,13 @@ def HighLevel_BinaryCondOp
let assemblyFormat = [{ attr-dict `:` type(results) $commonRegion `,` $condRegion `?` $thenRegion `:` $elseRegion }];
}

def HighLevel_WhileOp
: HighLevel_ControlFlowOp< "while", [NoTerminator] >
{
def HighLevel_WhileOp : HighLevel_ControlFlowOp< "while", [
NoTerminator, Core_ScopeLikeTrait,
Core_ShadowingSymbolTable< [
[Core_VarSymbol, Core_TypeSymbol, Core_EnumConstantSymbol],
[Core_ElaboratedTypeSymbol]
] >
] > {
let summary = "VAST while statement";
let description = [{
The operation takes builders of two mandatory regions -- condition and body
Expand Down Expand Up @@ -328,9 +336,13 @@ def HighLevel_WhileOp
}


def HighLevel_ForOp
: HighLevel_ControlFlowOp< "for" >
{
def HighLevel_ForOp : HighLevel_ControlFlowOp< "for", [
NoTerminator, Core_ScopeLikeTrait,
Core_ShadowingSymbolTable< [
[Core_VarSymbol, Core_TypeSymbol, Core_EnumConstantSymbol],
[Core_ElaboratedTypeSymbol]
] >
] > {
let summary = "VAST for statement";
let description = [{
Operation represents a for-loop statement.
Expand Down Expand Up @@ -367,9 +379,14 @@ def HighLevel_ForOp
}];
}

def HighLevel_DoOp
: HighLevel_ControlFlowOp< "do" >
{
def HighLevel_DoOp : HighLevel_ControlFlowOp< "do", [
NoTerminator, Core_ScopeLikeTrait,
// FIXME: cond region should not expose symbols to the body region
Core_ShadowingSymbolTable< [
[Core_VarSymbol, Core_TypeSymbol, Core_EnumConstantSymbol],
[Core_ElaboratedTypeSymbol]
] >
] > {
let summary = "VAST do-while statement";
let description = [{
The operation represents a do-while statement.
Expand Down Expand Up @@ -420,9 +437,14 @@ def HighLevel_ContinueOp
let assemblyFormat = [{ attr-dict }];
}

def HighLevel_SwitchOp
: HighLevel_ControlFlowOp< "switch" >
{
def HighLevel_SwitchOp : HighLevel_ControlFlowOp< "switch", [
NoTerminator, Core_ScopeLikeTrait,
// FIXME: look at proper scoping
Core_ShadowingSymbolTable< [
[Core_VarSymbol, Core_TypeSymbol, Core_EnumConstantSymbol],
[Core_ElaboratedTypeSymbol]
] >
] > {
let summary = "VAST switch statement";
let description = [{
The operation represents a switch statement.
Expand Down
1 change: 1 addition & 0 deletions include/vast/Dialect/HighLevel/HighLevelOps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ VAST_UNRELAX_WARNINGS

#include "vast/Dialect/Core/SymbolTable.hpp"

#include "vast/Dialect/Core/Interfaces/OperationInterfaces.hpp"
#include "vast/Dialect/Core/Interfaces/SymbolInterface.hpp"
#include "vast/Dialect/Core/Interfaces/TypeDefinitionInterface.hpp"

Expand Down
12 changes: 9 additions & 3 deletions include/vast/Dialect/HighLevel/HighLevelOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ include "mlir/IR/BuiltinAttributes.td"
include "mlir/Interfaces/CastInterfaces.td"

include "vast/Interfaces/TypeTraitExprInterface.td"
include "vast/Dialect/Core/Interfaces/OperationInterfaces.td"
include "vast/Dialect/Core/Interfaces/SymbolInterface.td"
include "vast/Dialect/Core/Interfaces/SymbolTableInterface.td"
include "vast/Dialect/Core/Interfaces/TypeDefinitionInterface.td"
Expand Down Expand Up @@ -420,8 +421,13 @@ def HighLevel_IndirectCallOp
// TODO: add verifiers to check that callee type matches arg operands
}

def HighLevel_ExprOp
: HighLevel_Op< "expr", [SingleBlock] >
def HighLevel_ExprOp: HighLevel_Op< "expr", [
NoTerminator, SingleBlock, Core_ScopeLikeTrait,
Core_ShadowingSymbolTable< [
[Core_VarSymbol, Core_TypeSymbol, Core_EnumConstantSymbol],
[Core_ElaboratedTypeSymbol]
] >
] >
, Results<(outs AnyType:$result)>
{
let summary = "VAST expression";
Expand Down Expand Up @@ -832,7 +838,7 @@ class HighLevel_IsSub< string lhs, string rhs, string res >
>;

class HighLevel_ArithBinOp< string mnemonic, list< Trait > traits = [] >
: HighLevel_Op< mnemonic, traits >
: HighLevel_Op< mnemonic, !listconcat(traits, [Core_ArithBinOp]) >
, Arguments<(ins AnyType:$lhs, AnyType:$rhs)>
, Results<(outs AnyType:$result)>
{
Expand Down
4 changes: 4 additions & 0 deletions include/vast/Dialect/Parser/Ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ VAST_RELAX_WARNINGS
VAST_UNRELAX_WARNINGS

#include "vast/Dialect/Parser/Dialect.hpp"
#include "vast/Dialect/Parser/Types.hpp"

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

#include "vast/Util/Common.hpp"

#define GET_OP_CLASSES
Expand Down
Loading