Skip to content

Commit 4a4be9f

Browse files
[ImportVerilog] Introduce the semantic analysis of slang
Initially, the intention was to check the validity of variable assignments. But in the end, introduce the semantic analysis of slang. However, only add test cases for variable assignments--it is illegal to assign to a variable with multiple continuous assignments or mix continuous and procedural assignments.
1 parent a0ce24f commit 4a4be9f

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

lib/Conversion/ImportVerilog/AssertionExpr.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,11 @@ Value Context::convertAssertionCallExpression(
426426
value = this->convertRvalueExpression(*args[0]);
427427
originalType = value.getType();
428428
valTy = dyn_cast<moore::IntType>(value.getType());
429+
430+
// The semantic analysis of slang will handle this error, so extra error
431+
// emission here is not necessary, but we need to check for it to avoid
432+
// crashes in case of malformed input.
429433
if (!valTy) {
430-
mlir::emitError(loc) << "expected integer argument for system call `"
431-
<< subroutine.name << "`";
432434
return {};
433435
}
434436
// If the value is four-valued, we need to map it to two-valued before we

lib/Conversion/ImportVerilog/ImportVerilog.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/ADT/Hashing.h"
2727
#include "llvm/Support/SourceMgr.h"
2828

29+
#include "slang/analysis/AnalysisManager.h"
2930
#include "slang/diagnostics/DiagnosticClient.h"
3031
#include "slang/driver/Driver.h"
3132
#include "slang/parsing/Preprocessor.h"
@@ -274,6 +275,11 @@ LogicalResult ImportDriver::importVerilog(ModuleOp module) {
274275
// Elaborate the input.
275276
auto compileTimer = ts.nest("Verilog elaboration");
276277
auto compilation = driver.createCompilation();
278+
279+
// Semantic analysis
280+
auto analysisTimer = ts.nest("Semantic analysis");
281+
driver.runAnalysis(*compilation);
282+
277283
for (auto &diag : compilation->getAllDiagnostics())
278284
driver.diagEngine.issue(diag);
279285
if (!parseSuccess || driver.diagEngine.getNumErrors() > 0)

test/Conversion/ImportVerilog/errors.sv

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ endfunction
180180
// -----
181181
module Foo;
182182
string b;
183-
// expected-error @below {{expected integer argument for system call `$past`}}
183+
// expected-error @below {{sequence has no explicit clocking event and one cannot be inferred from context}}
184184
assert property ($past(b));
185185
endmodule
186186

@@ -200,3 +200,52 @@ module Foo;
200200
s[0] = b;
201201
end
202202
endmodule
203+
204+
// -----
205+
module Foo;
206+
int v = 1;
207+
208+
// expected-error @+2 {{cannot mix continuous and procedural assignments to variable 'v'}}
209+
// expected-remark @-3 {{also assigned here}}
210+
assign v = 12;
211+
endmodule
212+
213+
// -----
214+
module Foo;
215+
int v;
216+
217+
// expected-error @+3 {{cannot have multiple continuous assignments to variable 'v'}}
218+
// expected-remark @below {{also assigned here}}
219+
assign v = 12;
220+
assign v = 13;
221+
endmodule
222+
223+
// -----
224+
module Foo;
225+
wire clk = 0;
226+
int v;
227+
228+
// expected-error @+3 {{cannot mix continuous and procedural assignments to variable 'v'}}
229+
// expected-remark @below {{also assigned here}}
230+
assign v = 12;
231+
always @(posedge clk) v <= ~v;
232+
endmodule
233+
234+
// -----
235+
module Foo;
236+
wire clk = 0;
237+
int v;
238+
239+
// expected-error @+3 {{cannot mix continuous and procedural assignments to variable 'v'}}
240+
// expected-remark @below {{also assigned here}}
241+
always @(posedge clk) v <= ~v;
242+
assign v = 12;
243+
endmodule
244+
245+
// -----
246+
module Foo;
247+
logic a;
248+
249+
// expected-error @below {{'always' procedure does not advance time and so will create a simulation deadlock}}
250+
always a = ~a;
251+
endmodule

test/Conversion/ImportVerilog/procedures.sv

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ module Foo;
1919
// CHECK-NEXT: }
2020
final foo();
2121

22-
// CHECK: moore.procedure always {
23-
// CHECK-NEXT: func.call @foo
24-
// CHECK-NEXT: moore.return
25-
// CHECK-NEXT: }
26-
always foo();
27-
2822
// CHECK: moore.procedure always_comb {
2923
// CHECK-NEXT: func.call @foo
3024
// CHECK-NEXT: moore.return

0 commit comments

Comments
 (0)