Skip to content
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
25 changes: 9 additions & 16 deletions mlir/include/mlir/Conversion/SolToYul/EVMUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#include <optional>

namespace mlir {
class TypeConverter;

namespace evm {

class StringLiteralPool;
Expand All @@ -38,14 +36,19 @@ enum ByteLen {

using AllocSize = int64_t;

/// Returns the type a value of `ty` has in Yul.
Type getYulType(Type ty);

/// Returns the alignment of the LLVMPointerType value `ptr`
unsigned getAlignment(Value ptr);

/// Returns true when \p v is a compile-time constant. The type converter may
/// leave a single materialization cast over the constant, which is looked
/// through.
/// Returns true when \p v is a compile-time constant.
bool isConstantValue(Value v);

/// Creates the private function `name` of type `ty`.
yul::FuncOp createPrivateFunc(OpBuilder &b, Location loc, StringRef name,
FunctionType ty);

// FIXME: Remove this! The lowering should not expand the ABI encoding/decoding
// code (where this is used) involved in sol.contract, sol.emit etc. lowering.
// Instead it should generate a high level sol op that can have a custom
Expand Down Expand Up @@ -128,9 +131,6 @@ class Builder {
this->literals = &literals;
}

//
// The following APIs are used in the stage1 lowering.
//
// TODO? Some of these could be transformed to mlir ops instead. This would
// allow targets to define custom lowering. For instance, some targets might
// not need the `type(uint64).max` check in the memory allocation code
Expand Down Expand Up @@ -389,11 +389,6 @@ class Builder {
bool fromMem, Location loc);

public:
//
// TODO? Should we work with the high level types + OpAdaptor for the APIs
// that work with low level integral type pointers?
//

/// Generates a low level integral type pointer to the address holding the
/// data of a dynamic allocation.
Value genDataAddrPtr(Value addr, Type ty,
Expand Down Expand Up @@ -696,11 +691,9 @@ class Builder {
bool topLevel,
std::optional<Location> locArg = std::nullopt);
/// Generates the tuple decoder code as per the ABI and populates the results.
/// A non-empty tuple is decoded by `helpersym::abiDecodeTuple`, whose
/// results are the types \p tyConv gives \p tys.
/// A non-empty tuple is decoded by `helpersym::abiDecodeTuple`.
void genABITupleDecoding(TypeRange tys, Value tupleStart, Value tupleEnd,
std::vector<Value> &results, bool fromMem,
const TypeConverter &tyConv,
std::optional<Location> locArg = std::nullopt);
/// Generates a panic with \p code through `helpersym::panic`.
void genPanic(PanicCode code, std::optional<Location> locArg = std::nullopt);
Expand Down
126 changes: 19 additions & 107 deletions mlir/include/mlir/Conversion/SolToYul/SolToYul.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//===- SolToYul.h - Sol to Yul dialect conversion ---------------*- C++ -*-===//
//===- SolToYul.h - Sol to Yul lowering -------------------------*- 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 declares the Sol-to-Yul conversion pass and its EVM-specific
// lowering patterns.
// This file declares the Sol to Yul lowering pass and the state it shares with
// the EVM utilities.
//
//===----------------------------------------------------------------------===//

Expand All @@ -17,9 +17,7 @@
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/Sol/Sol.h"
#include "mlir/Dialect/Yul/Yul.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/DialectConversion.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SetVector.h"
Expand All @@ -32,40 +30,25 @@ namespace mlir {

namespace evm {

class SolTypeConverter : public TypeConverter {
public:
SolTypeConverter();
};

/// The internal function pointer dispatch of a module. Inline assembly can
/// The internal function pointer dispatch of a contract. Inline assembly can
/// retype a pointer, so a call reaches every target that takes and returns as
/// many words. The pass records its contract's creation_func_refs before
/// stage 2 erases the contract. Stage 1 records each sol.func_constant's target
/// and turns each sol.icall into a call of its dispatch function, one per
/// lowered signature, declared at its first call. The pass gives the dispatch
/// functions their bodies after stage 2 has moved the functions into their
/// object.
/// many words. Each call goes through the dispatch function of its signature,
/// declared at its first call; the bodies are generated once every target is
/// known.
class InternalDispatch {
public:
/// The words a function takes and returns.
using WordCounts = std::pair<unsigned, unsigned>;

explicit InternalDispatch(const TypeConverter &tyConv) : tyConv(tyConv) {}
/// Records `fn`, of pointer value `id`, as a target of the calls that take
/// and return as many words as it does.
void addTarget(yul::FuncOp fn, int64_t id);

/// Records the functions `contr` lists in its creation_func_refs.
void addCreationFuncRefs(sol::ContractOp contr);

/// Records `fn` as a target of the calls that take and return as many words
/// as it does.
void addTarget(sol::FuncOp fn);

/// Returns the dispatch function `icall` calls, of `dispatchFnTy`: the
/// pointer ahead of the callee's arguments, to the callee's results. The
/// first call of that signature declares it, so every call names an existing
/// function.
yul::FuncOp getOrCreateDispatchFn(sol::ICallOp icall,
FunctionType dispatchFnTy,
PatternRewriter &r);
/// Returns the dispatch function of `dispatchFnTy`: the pointer ahead of the
/// callee's arguments, to the callee's results. The first call, from
/// `caller`, declares it.
yul::FuncOp getOrCreateDispatchFn(FunctionType dispatchFnTy,
yul::FuncOp caller, OpBuilder &b);

/// Gives each declared dispatch function its body, a switch over the targets
/// of its word counts, and places it after its first caller.
Expand All @@ -74,13 +57,14 @@ class InternalDispatch {
private:
struct Request {
yul::FuncOp fn;
sol::FuncOp caller;
yul::FuncOp caller;
};

const TypeConverter &tyConv;
/// The targets of the calls of each word counts, in the order they are
/// recorded, which the cases of a dispatch follow.
DenseMap<WordCounts, llvm::SmallSetVector<sol::FuncOp, 4>> targets;
DenseMap<WordCounts, llvm::SmallSetVector<yul::FuncOp, 4>> targets;
/// The pointer value of each target.
DenseMap<yul::FuncOp, int64_t> ids;
llvm::MapVector<FunctionType, Request> requests;
};

Expand All @@ -98,78 +82,6 @@ class StringLiteralPool {
llvm::StringMap<LLVM::GlobalOp> globals;
};

/// Adds the conversion patterns of unchecked arithmetic ops in the sol dialect.
void populateArithPats(RewritePatternSet &pats, TypeConverter &tyConv);

/// Adds the conversion patterns of internal function pointers: the constants
/// and the calls through them, which `dispatch` records.
void populateInternalFuncPtrPats(RewritePatternSet &pats, TypeConverter &tyConv,
InternalDispatch &dispatch);

/// Adds the conversion patterns of crypto ops in the sol dialect. Literal
/// inputs take their globals from `literals`.
void populateCryptoPats(RewritePatternSet &pats, TypeConverter &tyConv,
StringLiteralPool &literals);

/// Adds the conversion patterns of checked arithmetic ops in the sol dialect.
void populateCheckedArithPats(RewritePatternSet &pats, TypeConverter &tyConv);

/// Adds the conversion patterns of sol dialect ops dealing with stack, memory
/// and storage allocations. String literals and literal arguments of
/// sol.concat take their globals from `literals`.
///
/// Stack access will have 32-byte alignment while all other access will have
/// 1-byte alignment.
void populateMemPats(RewritePatternSet &pats, TypeConverter &tyConv,
StringLiteralPool &literals);

/// Adds the conversion pattern of sol.emit. Literal arguments take their
/// globals from `literals`.
void populateEmitPat(RewritePatternSet &pats, TypeConverter &tyConv,
StringLiteralPool &literals);

/// Adds the pattern that erases a sol.constant_string, which its consumers
/// leave unused once they are lowered.
void populateConstantStringPat(RewritePatternSet &pats);

/// Adds the conversion pattern of sol.require. Literal reasons take their
/// globals from `literals`.
void populateRequirePat(RewritePatternSet &pats, StringLiteralPool &literals);

/// Adds the conversion patterns for address/block/tx/msg/gas-related ops.
void populateAddrPat(RewritePatternSet &pats, TypeConverter &tyConv);

/// Adds the conversion patterns of abi ops. Encoded literals take their
/// globals from `literals`.
void populateAbiPats(RewritePatternSet &pats, TypeConverter &tyConv,
StringLiteralPool &literals);

/// Adds the conversion pattern of sol.ext_call. Literal arguments and inputs
/// take their globals from `literals`.
void populateExtCallPat(RewritePatternSet &pats, TypeConverter &tyConv,
StringLiteralPool &literals);

/// Adds the conversion patterns of control flow ops in the sol dialect.
void populateControlFlowPats(RewritePatternSet &pats, TypeConverter &tyConv);

/// Adds the conversion patterns of sol.call and sol.return.
void populateFuncBoundaryPats(RewritePatternSet &pats, TypeConverter &tyConv);

/// Adds the conversion pattern of sol.func.
void populateFuncOpPats(RewritePatternSet &pats, TypeConverter &tyConv);

/// Adds the conversion pattern of sol.contract.
void populateContractPat(RewritePatternSet &pats, const TypeConverter &tyConv);

/// Adds patterns for sol.inline_asm and related ops.
void populateInlineAsmPats(RewritePatternSet &pats, TypeConverter &tyConv);

void populateStage1Pats(RewritePatternSet &pats, TypeConverter &tyConv,
InternalDispatch &dispatch,
StringLiteralPool &literals);

void populateStage2Pats(RewritePatternSet &pats, const TypeConverter &tyConv);

} // namespace evm
} // namespace mlir

Expand Down
8 changes: 8 additions & 0 deletions mlir/include/mlir/Conversion/SolToYul/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ class BuilderExt {
return genConst(llvm::APInt(256, val, /*isSigned=*/true), locArg);
}

/// Generates `val`, a value of `ty`, extended to 256 bits by the signedness
/// of `ty`.
Value genI256Const(llvm::APInt const &val, IntegerType ty,
std::optional<Location> locArg = std::nullopt) {
assert(val.getBitWidth() <= 256 && "Expected constant width <= 256");
return genConst(ty.isSigned() ? val.sext(256) : val.zext(256), locArg);
}

/// Generates an i256 constant with the selector in the high 32 bits.
Value genI256Selector(StringRef signature,
std::optional<Location> locArg = std::nullopt) {
Expand Down
13 changes: 0 additions & 13 deletions mlir/include/mlir/Dialect/Sol/SolOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,6 @@ include "mlir/IR/SymbolInterfaces.td"
// specific ops) should verify that their types (ssa argument types and
// TypeAttr) are high level ((un)signed int types, sol dialect types etc.).

// FIXME: Remove this! We're already running reconcile-unrealized-casts, so we
// should start using builtin.unrealized_conversion_cast
def Sol_ConvCastOp : Sol_Op<"conv_cast", [Pure]> {
let summary = "No-op cast that casts to the remapped value in conversion";

let arguments = (ins AnyType:$inp);
let results = (outs AnyType:$out);

let assemblyFormat = "$inp attr-dict `:` type($inp) `->` type($out)";

let hasFolder = 1;
}

def Sol_ConstantOp : Op<Sol_Dialect, "constant",
[ConstantLike, Pure,
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>,
Expand Down
4 changes: 0 additions & 4 deletions mlir/lib/Conversion/SolToYul/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ add_mlir_conversion_library(MLIRSolToYul
HelperSymbols.cpp
InternalDispatch.cpp
SolToYul.cpp
SolToYulPass.cpp
TypeConverter.cpp
Util.cpp

ADDITIONAL_HEADER_DIRS
Expand All @@ -18,8 +16,6 @@ add_mlir_conversion_library(MLIRSolToYul
MLIRYulDialect
MLIRLLVMDialect
MLIRPass
MLIRTransforms
MLIRTransformUtils

LINK_COMPONENTS
Support
Expand Down
Loading
Loading