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

Convert ICE40 to BLIF #8031

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
17 changes: 17 additions & 0 deletions docs/Dialects/ICE40/RationaleICE40.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# `ice40` Dialect Rationale

This document describes various design points of the ice40 dialect, a
dialect that matches the technology library used in the Lattice ICE40 family
of FPGAs.

## Introduction to the `ice40` Dialect

The `ice40` dialect provides a collection of operations that match the
documented operations for the ICE40 family of FPGAs. The dialect hues closely
to the documentation.

The intent of the dialect is as a target of synthesis.

## Type System for `ice40` Dialect

The dialect uses signless 1-bit integers for all values.
24 changes: 24 additions & 0 deletions include/circt/Conversion/ICE40ToBLIF.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===- ICE40ToBLIF.h - ICE40 to BLIF dialect conversion ---------*- 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
//
//===----------------------------------------------------------------------===//

#ifndef CIRCT_CONVERSION_ICE40TOBLIF_H
#define CIRCT_CONVERSION_ICE40TOBLIF_H

#include "circt/Support/LLVM.h"
#include <memory>

namespace circt {

std::unique_ptr<mlir::Pass> createICE40ToBLIFPass();

#define GEN_PASS_DECL_ICE40TOBLIF
#include "circt/Conversion/Passes.h.inc"

} // namespace circt

#endif // CIRCT_CONVERSION_ICE40TOBLIF_H
1 change: 1 addition & 0 deletions include/circt/Conversion/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "circt/Conversion/HWToSystemC.h"
#include "circt/Conversion/HandshakeToDC.h"
#include "circt/Conversion/HandshakeToHW.h"
#include "circt/Conversion/ICE40ToBLIF.h"
#include "circt/Conversion/LTLToCore.h"
#include "circt/Conversion/LoopScheduleToCalyx.h"
#include "circt/Conversion/MooreToCore.h"
Expand Down
14 changes: 14 additions & 0 deletions include/circt/Conversion/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,20 @@ def HandshakeToHW : Pass<"lower-handshake-to-hw", "mlir::ModuleOp"> {
"seq::SeqDialect"];
}

//===----------------------------------------------------------------------===//
// ICE40ToBLIF
//===----------------------------------------------------------------------===//

def ICE40ToBLIF : Pass<"lower-ice40-to-blif", "mlir::ModuleOp"> {
let summary = "Lower ICE40 to BLIF";
let description = [{
This pass lowers ICE40 to BLIF.
}];
let constructor = "circt::createICE40ToBLIFPass()";
let dependentDialects = ["ice40::ICE40Dialect", "hw::HWDialect",
"blif::BLIFDialect"];
}

//===----------------------------------------------------------------------===//
// Moore to Comb/HW/LLHD
//===----------------------------------------------------------------------===//
Expand Down
21 changes: 21 additions & 0 deletions include/circt/Dialect/BLIF/BLIF.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===- BLIF.td - BLIF dialect definition -------------------*- tablegen -*-===//
//
// 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 is the top level file for the BLIF dialect. This dialect represents
// the (extended) Berkely Logic Interchange Format
// [http://bear.ces.cwru.edu/eecs_cad/sis_blif.pdf].
//
//===----------------------------------------------------------------------===//

#ifndef CIRCT_DIALECT_BLIF_BLIF_TD
#define CIRCT_DIALECT_BLIF_BLIF_TD

include "circt/Dialect/BLIF/BLIFDialect.td"
include "circt/Dialect/BLIF/BLIFOps.td"

#endif // CIRCT_DIALECT_BLIF_BLIF_TD
24 changes: 24 additions & 0 deletions include/circt/Dialect/BLIF/BLIFDialect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===- BLIFDialect.h - BLIF dialect declaration -----------------*- 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 defines an BLIF MLIR dialect.
//
//===----------------------------------------------------------------------===//

#ifndef CIRCT_DIALECT_BLIF_BLIFDIALECT_H
#define CIRCT_DIALECT_BLIF_BLIFDIALECT_H

#include "circt/Support/LLVM.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/Dialect.h"

// Pull in the dialect definition.
#include "circt/Dialect/BLIF/BLIFDialect.h.inc"
#include "circt/Dialect/BLIF/BLIFEnums.h.inc"

#endif // CIRCT_DIALECT_BLIF_BLIFDIALECT_H
33 changes: 33 additions & 0 deletions include/circt/Dialect/BLIF/BLIFDialect.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===- BLIFDialect.td - BLIF dialect definition ------------*- tablegen -*-===//
//
// 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 contains the BLIFDialect definition to be included in other files.
//
//===----------------------------------------------------------------------===//

#ifndef CIRCT_DIALECT_BLIF_BLIFDIALECT
#define CIRCT_DIALECT_BLIF_BLIFDIALECT

include "mlir/IR/OpBase.td"

def BLIFDialect : Dialect {
let name = "blif";
let cppNamespace = "::circt::blif";

let summary = "Types and operations for the BLIF format dialect";
let description = [{
This dialect defines the `BLIF` dialect, which is a representation of
the Berkely Logic Interchange Format.
}];
}

// Base class for the operation in this dialect.
class BLIFOp<string mnemonic, list<Trait> traits = []> :
Op<BLIFDialect, mnemonic, traits>;

#endif // CIRCT_DIALECT_BLIF_BLIFDIALECT
30 changes: 30 additions & 0 deletions include/circt/Dialect/BLIF/BLIFEmitter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===- BLIFEmitter.h - BLIF dialect to .blif emitter ------------*- 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
//
//===----------------------------------------------------------------------===//
//
// Defines the interface to the .blif file emitter.
//
//===----------------------------------------------------------------------===//

#ifndef CIRCT_DIALECT_BLIF_BLIFEMITTER_H
#define CIRCT_DIALECT_BLIF_BLIFEMITTER_H

#include "circt/Support/LLVM.h"

namespace circt {
namespace blif {

mlir::LogicalResult exportBLIFFile(mlir::ModuleOp module,
llvm::raw_ostream &os);

void registerToBLIFFileTranslation();

} // namespace blif
} // namespace circt

#endif // CIRCT_DIALECT_BLIF_BLIFEMITTER_H
27 changes: 27 additions & 0 deletions include/circt/Dialect/BLIF/BLIFOps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===- BLIFOps.h - Declare BLIF dialect operations --------------*- 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 operation classes for the BLIF dialect.
//
//===----------------------------------------------------------------------===//

#ifndef CIRCT_DIALECT_BLIF_BLIFOPS_H
#define CIRCT_DIALECT_BLIF_BLIFOPS_H

#include "circt/Dialect/BLIF/BLIFDialect.h"
#include "circt/Dialect/HW/HWOpInterfaces.h"
#include "mlir/Bytecode/BytecodeOpInterface.h"
#include "mlir/IR/OpImplementation.h"

#define GET_OP_CLASSES
#include "circt/Dialect/BLIF/BLIF.h.inc"

#endif // CIRCT_DIALECT_BLIF_BLIFOPS_H

using namespace circt;
using namespace blif;
97 changes: 97 additions & 0 deletions include/circt/Dialect/BLIF/BLIFOps.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//===- BLIFOps.td - BLIF ops ============-------------------*- tablegen -*-===//
//
// 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 defines the BLIF ops. This defines module-like operations, connections,
// and logic.
//===----------------------------------------------------------------------===//

#ifndef CIRCT_DIALECT_BLIF_BLIFOPS_TD
#define CIRCT_DIALECT_BLIF_BLIFOPS_TD

include "circt/Dialect/BLIF/BLIFDialect.td"
include "circt/Dialect/HW/HWTypes.td"
include "mlir/IR/EnumAttr.td"
include "mlir/IR/RegionKindInterface.td"

def ModelOp : BLIFOp<"model", [IsolatedFromAbove, RegionKindInterface, SingleBlockImplicitTerminator<"OutputOp">]> {
let summary = "A model, which is a module";
let description = [{
The basic container. Is like a module.
}];

let arguments = (ins SymbolNameAttr:$sym_name,
TypeAttrOf<ModuleType>:$module_type,
APIntAttr:$clocks);
let results = (outs);
let regions = (region SizedRegion<1>:$body);

let builders = [
OpBuilder<(ins "StringRef":$name,
"ArrayRef<StringRef>":$inputs,
"ArrayRef<StringRef>":$outputs,
"ArrayRef<StringRef>":$clocks)>
];


let extraClassDeclaration = [{
static mlir::RegionKind getRegionKind(unsigned index) {
return mlir::RegionKind::Graph;
}
}];
}

def OutputOp : BLIFOp<"output", [Terminator]> {
let arguments = (ins Variadic<I1>:$inputs);
}

def I8Property : IntProperty<"int8_t">;

def LogicGateOp: BLIFOp<"logic_gate", []> {
let summary = "Combinatorial logic";
let description = [{
A logic gate represents a logic function in sum-of-products
form. Each entry in $func is a vector applied to the inputs
where 0 means invert the input, 1 means use the input, and 2
means don't use the input.
}];

let arguments = (ins ArrayProperty<IntArrayProperty<I8Property>>:$func,
Variadic<I1>:$inputs);
let results = (outs I1:$result);
}

def LatchMode : I32EnumAttr<
"LatchModeEnum", "Latch Mode", [
I32EnumAttrCase<"Unspecified", 0, "un">,
I32EnumAttrCase<"FallingEdge", 1, "fe">,
I32EnumAttrCase<"RisingEdge", 2, "re">,
I32EnumAttrCase<"ActiveHigh", 3, "ah">,
I32EnumAttrCase<"ActiveLow", 4, "al">,
I32EnumAttrCase<"Asynchronous", 5, "as">
]> {
let cppNamespace = "circt::blif";
}

def LatchGateOp: BLIFOp<"latch_gate", []> {

let arguments = (ins I1:$input, LatchMode:$mode, Optional<I1>:$clock, I32Property:$initVal);
let results = (outs I1:$output);
}

def LibraryGateOp: BLIFOp<"library_gate", []> {
let summary = "Library logic";
let description = [{
}];

let arguments = (ins FlatSymbolRefAttr:$libraryName,
Variadic<I1>:$inputs);
let results = (outs I1:$result);
}


#endif // CIRCT_DIALECT_BLIF_BLIFOPS_TD
61 changes: 61 additions & 0 deletions include/circt/Dialect/BLIF/BLIFParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//===- BLIFParser.h - .blif to BLIF dialect parser --------------*- 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
//
//===----------------------------------------------------------------------===//
//
// Defines the interface to the .blif file parser.
//
//===----------------------------------------------------------------------===//

#ifndef CIRCT_DIALECT_BLIF_BLIFPARSER_H
#define CIRCT_DIALECT_BLIF_BLIFPARSER_H

#include "circt/Support/LLVM.h"
#include <optional>
#include <string>
#include <vector>

namespace llvm {
class SourceMgr;
} // namespace llvm

namespace mlir {
class LocationAttr;
class TimingScope;
} // namespace mlir

namespace circt {
namespace blif {

struct BLIFParserOptions {
/// Specify how @info locators should be handled.
enum class InfoLocHandling {
/// If this is set to true, the @info locators are ignored, and the
/// locations are set to the location in the .BLIF file.
IgnoreInfo,
/// Prefer @info locators, fallback to .BLIF locations.
PreferInfo,
/// Attach both @info locators (when present) and .BLIF locations.
FusedInfo
};

InfoLocHandling infoLocatorHandling = InfoLocHandling::PreferInfo;

/// parse strict blif instead of extended blif
bool strictBLIF = false;
};

mlir::OwningOpRef<mlir::ModuleOp>
importBLIFFile(llvm::SourceMgr &sourceMgr, mlir::MLIRContext *context,
mlir::TimingScope &ts, BLIFParserOptions options = {});

void registerFromBLIFFileTranslation();

} // namespace blif
} // namespace circt

#endif // CIRCT_DIALECT_BLIF_BLIFPARSER_H
7 changes: 7 additions & 0 deletions include/circt/Dialect/BLIF/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
add_circt_dialect(BLIF blif)

set(LLVM_TARGET_DEFINITIONS BLIF.td)
mlir_tablegen(BLIFEnums.h.inc -gen-enum-decls)
mlir_tablegen(BLIFEnums.cpp.inc -gen-enum-defs)
add_public_tablegen_target(CIRCTBLIFEnumsIncGen)
add_dependencies(circt-headers CIRCTBLIFEnumsIncGen)
2 changes: 2 additions & 0 deletions include/circt/Dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

add_subdirectory(AIG)
add_subdirectory(Arc)
add_subdirectory(BLIF)
add_subdirectory(Calyx)
add_subdirectory(Comb)
add_subdirectory(DC)
Expand All @@ -19,6 +20,7 @@ add_subdirectory(FSM)
add_subdirectory(Handshake)
add_subdirectory(HW)
add_subdirectory(HWArith)
add_subdirectory(ICE40)
add_subdirectory(Interop)
add_subdirectory(LLHD)
add_subdirectory(LoopSchedule)
Expand Down
3 changes: 3 additions & 0 deletions include/circt/Dialect/ICE40/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_circt_dialect(ICE40 ice40)

set(LLVM_TARGET_DEFINITIONS ICE40.td)
Loading
Loading