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

[Moore] Add evenOp to handle event controls. #7154

Merged
merged 1 commit into from
Jun 14, 2024
Merged
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
59 changes: 59 additions & 0 deletions include/circt/Dialect/Moore/MooreOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,65 @@ def NonBlockingAssignOp : AssignOpBase<"nonblocking_assign"> {
}];
}

//===----------------------------------------------------------------------===//
// Statements
//===----------------------------------------------------------------------===//

def None: I32EnumAttrCase<"None", 0, "none">;
/// Transit from 0 to x/z/1, and from x/z to 1.
def PosEdge: I32EnumAttrCase<"PosEdge", 1, "posedge">;
/// Transit from 1 to x/z/0, and from x/z to 0.
def NegEdge: I32EnumAttrCase<"NegEdge", 2, "negedge">;
/// Include the negedge and posedge.
def BothEdges: I32EnumAttrCase<"BothEdges", 3, "edge">;

def EdgeAtrr: I32EnumAttr<"Edge", "Edge kind",
[None, PosEdge, NegEdge, BothEdges]>{
let cppNamespace = "circt::moore";
}

def EventOp : MooreOp<"wait_event", [
HasParent<"ProcedureOp">
]> {
let summary = "Detecting posedge and negedge";
let description = [{
It is introduced by the symbol `@`, and it allows statement execution to
be delayed until the occurrence of some simulation event occurring in a
procedure executing concurrently with this procedure.

For the implicit event control(`@(*)`), there are two situations that are
not automatically added to event expression:
1. Identifiers that only appear in wait or event expressions.
```
always @(*) begin // equivalent to @(b)
@(n) kid = b; // n is not added to @(*)
end
```
2. Identifiers that only appear as a hierarchical_variable_identifier
in the variable_lvalue of the left-hand side of assignments.
```
always @(*) begin
a = b + c; // a is not added to @(*)
end
```

Example:
```
@(a, b, c) // none
@(posedge clk) // positive edge
@(negedge clk) // negative edge
@(edge clk) // both edge
@(*) // implicit event control
```
See IEEE 1800-2017 § 9.4.2 "Event control".
}];
let arguments = (ins EdgeAtrr:$edge, UnpackedType:$input);
let results = (outs);
let assemblyFormat = [{
$edge $input attr-dict `:` type($input)
}];
}

//===----------------------------------------------------------------------===//
// Expressions
//===----------------------------------------------------------------------===//
Expand Down
1 change: 1 addition & 0 deletions lib/Conversion/ImportVerilog/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ add_circt_translation_library(CIRCTImportVerilog
Statements.cpp
Structure.cpp
Types.cpp
TimingControls.cpp

DEPENDS
slang_slang
Expand Down
4 changes: 4 additions & 0 deletions lib/Conversion/ImportVerilog/ImportVerilogInternals.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ struct Context {
Value convertRvalueExpression(const slang::ast::Expression &expr);
Value convertLvalueExpression(const slang::ast::Expression &expr);

// Convert a slang timing control into an MLIR timing control.
LogicalResult
convertTimingControl(const slang::ast::TimingControl &timingControl);

mlir::ModuleOp intoModuleOp;
const slang::SourceManager &sourceManager;
SmallDenseMap<slang::BufferID, StringRef> &bufferFilePaths;
Expand Down
6 changes: 5 additions & 1 deletion lib/Conversion/ImportVerilog/Statements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,12 @@ struct StmtVisitor {
return success();
}

// Ignore timing control for now.
// Handle timing control.
LogicalResult visit(const slang::ast::TimedStatement &stmt) {
if (failed(context.convertTimingControl(stmt.timing)))
return failure();
if (failed(context.convertStatement(stmt.stmt)))
return failure();
return success();
}

Expand Down
64 changes: 64 additions & 0 deletions lib/Conversion/ImportVerilog/TimingControls.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//===- TimingControl.cpp - Slang timing control conversion ----------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "ImportVerilogInternals.h"
#include "slang/ast/TimingControl.h"
using namespace circt;
using namespace ImportVerilog;
namespace {
struct TimingCtrlVisitor {
Context &context;
Location loc;
OpBuilder &builder;

TimingCtrlVisitor(Context &context, Location loc)
: context(context), loc(loc), builder(context.builder) {}

LogicalResult visit(const slang::ast::SignalEventControl &ctrl) {
// TODO: When updating slang to the latest version, we will handle
// "iffCondition".
auto loc = context.convertLocation(ctrl.sourceRange.start());
auto input = context.convertRvalueExpression(ctrl.expr);
builder.create<moore::EventOp>(loc, static_cast<moore::Edge>(ctrl.edge),
input);
return success();
}

LogicalResult visit(const slang::ast::ImplicitEventControl &ctrl) {
return success();
}

LogicalResult visit(const slang::ast::EventListControl &ctrl) {
for (auto *event : ctrl.as<slang::ast::EventListControl>().events) {
if (failed(context.convertTimingControl(*event)))
return failure();
}
return success();
}

/// Emit an error for all other timing controls.
template <typename T>
LogicalResult visit(T &&node) {
mlir::emitError(loc, "unspported timing control: ")
<< slang::ast::toString(node.kind);
return failure();
}

LogicalResult visitInvalid(const slang::ast::TimingControl &ctrl) {
mlir::emitError(loc, "invalid timing control");
return failure();
}
};
} // namespace

LogicalResult
Context::convertTimingControl(const slang::ast::TimingControl &timingControl) {
auto loc = convertLocation(timingControl.sourceRange.start());
TimingCtrlVisitor visitor{*this, loc};
return timingControl.visit(visitor);
}
47 changes: 47 additions & 0 deletions test/Conversion/ImportVerilog/basic.sv
Original file line number Diff line number Diff line change
Expand Up @@ -1161,3 +1161,50 @@ module MultiPorts(
// CHECK: [[C1_READ:%.+]] = moore.read %c1
// CHECK: moore.output [[V1_READ]], [[C1_READ]]
endmodule

// CHECK-LABEL: moore.module @EventControl(in %clk : !moore.l1)
module EventControl(input clk);
// CHECK: %clk_0 = moore.net name "clk" wire : <l1>

int a1, a2, b, c;

// CHECK: moore.procedure always
// CHECK: [[CLK_READ:%.+]] = moore.read %clk_0 : l1
// CHECK: moore.wait_event posedge [[CLK_READ]] : l1
always @(posedge clk) begin end;

// CHECK: moore.procedure always
// CHECK: [[CLK_READ:%.+]] = moore.read %clk_0 : l1
// CHECK: moore.wait_event negedge [[CLK_READ]] : l1
always @(negedge clk) begin end;

// CHECK: moore.procedure always
// CHECK: [[CLK_READ:%.+]] = moore.read %clk_0 : l1
// CHECK: moore.wait_event edge [[CLK_READ]] : l1
always @(edge clk) begin end;
Comment on lines +1171 to +1184
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool! Can you add a test for something like always @(posedge x, negedge y iff z)? If we don't support those yet -- which would be totally fine -- we should at least emit an error message. I think this might be the case already, but it would be great to check that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice the iff(conditional event controls) is supported at slang v0.6, but nothing in the slang AST. For example:

module Top(input x, input y);
  logic cond;
  reg r;
  always @(posedge x, negedge y iff cond) begin
          r <= 1;
  end
endmodule
 {
              "name": "",
              "kind": "ProceduralBlock",
              "addr": 2199025173672,
              "procedureKind": "Always",
              "body": {
                "kind": "Timed",
                "timing": {
                  "kind": "EventList",
                  "events": [
                    {
                      "kind": "SignalEvent",
                      "expr": {
                        "kind": "NamedValue",
                        "type": "logic",
                        "symbol": "2199025174120 x"
                      },
                      "edge": "PosEdge"
                    },
                    {
                      "kind": "SignalEvent",
                      "expr": {
                        "kind": "NamedValue",
                        "type": "logic",
                        "symbol": "2199025174696 y"
                      },
                      "edge": "NegEdge"
                    }
                  ]
                },
                "stmt": {
                  "kind": "Block",
                  "blockKind": "Sequential",
                  "body": {
                    "kind": "ExpressionStatement",
                    "expr": {
                      "kind": "Assignment",
                      "type": "reg",
                      "left": {
                        "kind": "NamedValue",
                        "type": "reg",
                        "symbol": "2199025173296 r"
                      },
                      ...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a bug in the iff condition processing, and there is no printing support for it in slang before the patch that was introduced after the 6th release.

Currently AST for your example looks like this:

{
  "design": {
    "name": "$root",
    "kind": "Root",
    "addr": 4792547490880,
    "members": [
      {
        "name": "",
        "kind": "CompilationUnit",
        "addr": 4792546809056
      },
      {
        "name": "Top",
        "kind": "Instance",
        "addr": 4792546810408,
        "body": {
          "name": "Top",
          "kind": "InstanceBody",
          "addr": 4792546809352,
          "members": [
            {
              "name": "x",
              "kind": "Port",
              "addr": 4792546810536,
              "type": "logic",
              "direction": "In",
              "internalSymbol": "4792546810664 x"
            },
            {
              "name": "x",
              "kind": "Net",
              "addr": 4792546810664,
              "type": "logic",
              "netType": {
                "name": "wire",
                "kind": "NetType",
                "addr": 4792547488640,
                "type": "logic"
              }
            },
            {
              "name": "y",
              "kind": "Port",
              "addr": 4792546811032,
              "type": "logic",
              "direction": "In",
              "internalSymbol": "4792546811160 y"
            },
            {
              "name": "y",
              "kind": "Net",
              "addr": 4792546811160,
              "type": "logic",
              "netType": {
                "name": "wire",
                "kind": "NetType",
                "addr": 4792547488640,
                "type": "logic"
              }
            },
            {
              "name": "cond",
              "kind": "Variable",
              "addr": 4792546809608,
              "type": "logic",
              "lifetime": "Static"
            },
            {
              "name": "r",
              "kind": "Variable",
              "addr": 4792546809952,
              "type": "reg",
              "lifetime": "Static"
            },
            {
              "name": "",
              "kind": "ProceduralBlock",
              "addr": 4792546810296,
              "procedureKind": "Always",
              "body": {
                "kind": "Timed",
                "timing": {
                  "kind": "EventList",
                  "events": [
                    {
                      "kind": "SignalEvent",
                      "expr": {
                        "kind": "NamedValue",
                        "type": "logic",
                        "symbol": "4792546810664 x"
                      },
                      "edge": "PosEdge"
                    },
                    {
                      "kind": "SignalEvent",
                      "expr": {
                        "kind": "NamedValue",
                        "type": "logic",
                        "symbol": "4792546811160 y"
                      },
                      "edge": "NegEdge",
                      "iff": {
                        "kind": "NamedValue",
                        "type": "logic",
                        "symbol": "4792546809608 cond"
                      }
                    }
                  ]
                },
                "stmt": {
                  "kind": "Block",
                  "blockKind": "Sequential",
                  "body": {
                    "kind": "ExpressionStatement",
                    "expr": {
                      "kind": "Assignment",
                      "type": "reg",
                      "left": {
                        "kind": "NamedValue",
                        "type": "reg",
                        "symbol": "4792546809952 r"
                      },
                      "right": {
                        "kind": "Conversion",
                        "type": "reg",
                        "operand": {
                          "kind": "Conversion",
                          "type": "logic signed[31:0]",
                          "operand": {
                            "kind": "IntegerLiteral",
                            "type": "int",
                            "value": "1",
                            "constant": "1"
                          },
                          "constant": "1"
                        },
                        "constant": "1'b1"
                      },
                      "isNonBlocking": true
                    }
                  }
                }
              }
            }
          ],
          "definition": "4792547151360 Top"
        },
        "connections": [
        ]
      }
    ]
  },
  "definitions": [
    {
      "name": "Top",
      "kind": "Definition",
      "addr": 4792547151360,
      "defaultNetType": "4792547488640 wire",
      "definitionKind": "Module",
      "defaultLifetime": "Static",
      "unconnectedDrive": "None"
    }
  ]
}

Copy link

@likeamahoney likeamahoney Jun 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

patch which fixed this is here - MikePopoloski/slang@2d42130

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh~ Thanks! I used slang that I had installed locally. It's my mistake.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, we use slang v3.0 as the CIRCT library at present. 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😅 Yeah, once we get a reasonably complete SV pipeline together, we can start to look into gradually bumping to newer versions of Slang. I tried going to v4 at some point, but the required C++ version was too high compared to the CI runners that CIRCT CI ran on. It's probably going to be easier to work against v3 until we have something that we're happy with, and then make up for the difference to v4/5/6 after that.


// CHECK: moore.procedure always {
// CHECK: [[B_READ:%.+]] = moore.read %b : i32
// CHECK: moore.wait_event none [[B_READ]] : i32
// CHECK: [[C_READ:%.+]] = moore.read %c : i32
// CHECK: moore.wait_event none [[C_READ]] : i32
always @(b, c) begin
// CHECK: [[B_READ:%.+]] = moore.read %b : i32
// CHECK: [[C_READ:%.+]] = moore.read %c : i32
// CHECK: [[ADD:%.+]] = moore.add [[B_READ]], [[C_READ]] : i32
// CHECK: moore.blocking_assign %a1, [[ADD]] : i32
a1 = b + c;
end;

// CHECK: moore.procedure always
always @(*) begin
// CHECK: [[B_READ:%.+]] = moore.read %b : i32
// CHECK: [[C_READ:%.+]] = moore.read %c : i32
// CHECK: [[ADD:%.+]] = moore.add [[B_READ]], [[C_READ]] : i32
// CHECK: moore.blocking_assign %a2, [[ADD]] : i32
a2 = b + c;
end

// CHECK: moore.assign %clk_0, %clk : l1
// CHECK: moore.output
endmodule
Loading