`ifdef vs $plusargs in SystemVerilog: Compile Time vs Run Time (Tested)
In short: `ifdef is decided when the code is compiled, so code inside a false `ifdef is not in the build at all and changing it needs a recompile. $test$plusargs and $value$plusargs are decided when the simulation runs, so you change behaviour from the command line with no recompile. Use `ifdef for things that change the structure, like widths or optional code, and plusargs for choices that differ from test to test.
Every regression uses both, often for the same kind of switch. Pick the wrong one and you either recompile for every test, or you discover that a plusarg can’t change the width of a bus.
In this lesson we start with one switch done both ways, then go further with a UVM bench of 8 checks, including a prefix trap that surprises a lot of people. I ran all of it on Synopsys VCS.
Start here: the simplest example
One TAV_DEBUG switch, written once with `ifdef and once with $test$plusargs. Run it with no options, then with both switches on.
// tav_plus_simple.sv : the smallest `ifdef vs $test$plusargs example (UVM 1.2 reporting only)
// The Art of Verification | theartofverification.com
// Run on Synopsys VCS X-2025.06-SP1 with UVM 1.2 (EDA Playground), 1 Dec 2026.
// EDA Playground: Synopsys VCS, UVM 1.2, top module "tav_top".
//
// Run it twice:
// run 1: no options at all
// run 2: Compile Options +define+TAV_DEBUG and Run Options +TAV_DEBUG
`include "uvm_macros.svh"
module tav_top;
import uvm_pkg::*;
initial begin
// Compile time: this line only exists in the build if +define+TAV_DEBUG was given
`ifdef TAV_DEBUG
`uvm_info("TAV_SIMPLE", "`ifdef TAV_DEBUG : ON (decided when compiling)", UVM_NONE)
`else
`uvm_info("TAV_SIMPLE", "`ifdef TAV_DEBUG : off (decided when compiling)", UVM_NONE)
`endif
// Run time: this code is always compiled, the check happens when the test runs
if ($test$plusargs("TAV_DEBUG"))
`uvm_info("TAV_SIMPLE", "$test$plusargs TAV_DEBUG: ON (decided when running)", UVM_NONE)
else
`uvm_info("TAV_SIMPLE", "$test$plusargs TAV_DEBUG: off (decided when running)", UVM_NONE)
end
endmodule
Here is what VCS printed in the two runs:
# Run 1: no options
UVM_INFO testbench.sv(20) @ 0: reporter [TAV_SIMPLE] `ifdef TAV_DEBUG : off (decided when compiling)
UVM_INFO testbench.sv(27) @ 0: reporter [TAV_SIMPLE] $test$plusargs TAV_DEBUG: off (decided when running)
# Run 2: +define+TAV_DEBUG (compile) and +TAV_DEBUG (run)
UVM_INFO testbench.sv(18) @ 0: reporter [TAV_SIMPLE] `ifdef TAV_DEBUG : ON (decided when compiling)
UVM_INFO testbench.sv(25) @ 0: reporter [TAV_SIMPLE] $test$plusargs TAV_DEBUG: ON (decided when running)
- Run 1: no options, so both say off.
- Run 2:
+define+TAV_DEBUGwent to the compiler and+TAV_DEBUGwent to the simulation, so both say ON. - Look at the line numbers: 20/27 in run 1, 18/25 in run 2. A different branch of the
`ifdefwas compiled each time. The other branch did not exist in that build.
That is the whole difference. The full bench below shows what each one can and cannot do.
What you will learn
- When
`ifdefand plusargs are each decided. - How to pass a value with
+define+NAME=VALUEand with$value$plusargs. - What happens when a plusarg is missing.
- The prefix trap in
$test$plusargs. - How UVM reads plusargs with
uvm_cmdline_processor.
Cheat sheet: `ifdef vs plusargs
`ifdef / `define | $test$plusargs / $value$plusargs | |
|---|---|---|
| Decided | At compile time | At run time |
| How you set it | +define+TAV_FAST to the compiler | +TAV_LEN=12 to the simulation |
| Change it | Recompile | Rerun |
| Can remove code from the build | Yes | No, all code is compiled |
| Can size a bus or array | Yes | No |
| Typical use | Optional blocks, widths, tool-specific code | Test choices, lengths, modes, debug switches |
Analogy: `ifdef is choosing which rooms to build into a house. Plusargs are choosing which lights to switch on after you move in.
+define+ by the compiler, plusargs by the running simulation.Going further: one bench, 8 checks
- Checks 1 and 2 are compile time: an
`ifdef, and a macro with a value that sizes a bus. - Checks 3 to 7 are run time:
$test$plusargs,$value$plusargswith%dand%s, a missing plusarg, and the prefix trap. - Check 8 reads the same plusarg through UVM’s
uvm_cmdline_processor. - Planted bug:
+BUG=1reads the length with%hinstead of%d.
Run it yourself: both files are on GitHub, with the expected output.
// tav_plus_uvm.sv : `ifdef vs $plusargs in SystemVerilog, a complete working example (UVM 1.2)
// The Art of Verification | theartofverification.com
// Run on Synopsys VCS X-2025.06-SP1 with UVM 1.2 (EDA Playground), 1 Dec 2026.
// EDA Playground: Synopsys VCS, UVM 1.2, top module "tav_top".
// Paste this whole file into testbench.sv and leave design.sv empty.
//
// Set these options for the checks to pass:
// Compile Options : +define+TAV_FAST +define+TAV_WIDTH=16
// Run Options : +TAV_VERBOSE +TAV_LEN=12 +TAV_MODE=burst
//
// What is in here:
// tav_plus_test 8 self-checks, each printed with `uvm_info and checked with `uvm_error
// 1-2 compile time: `ifdef and a macro with a value
// 3-7 run time: $test$plusargs, $value$plusargs with %d and %s, a missing value, the prefix trap
// 8 UVM: uvm_cmdline_processor reads the same plusarg
//
// Planted bug:
// add +BUG=1 to the Run Options: TAV_LEN is read with %h instead of %d, so "12" becomes 18 (check 5 fails)
`include "uvm_macros.svh"
`ifndef TAV_WIDTH
`define TAV_WIDTH 8 // default when +define+TAV_WIDTH=... is not given
`endif
package tav_plus_pkg;
import uvm_pkg::*;
`include "uvm_macros.svh"
class tav_plus_test extends uvm_test;
`uvm_component_utils(tav_plus_test)
int tav_pass, tav_fail, tav_bug;
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
function void build_phase(uvm_phase phase);
super.build_phase(phase);
void'($value$plusargs("BUG=%d", tav_bug));
endfunction
function void tav_check(string what, string got, string exp);
if (got == exp) begin
tav_pass++;
`uvm_info("TAV_PLUS", $sformatf("PASS %s: %s", what, got), UVM_LOW)
end
else begin
tav_fail++;
`uvm_error("TAV_PLUS", $sformatf("FAIL %s: got %s, expected %s", what, got, exp))
end
endfunction
task run_phase(uvm_phase phase);
string tav_build, tav_mode, tav_mode_uvm;
bit [`TAV_WIDTH-1:0] tav_bus;
int tav_len = 4; // defaults, used if the plusarg is missing
int tav_addr = 'h1000;
int tav_got_len, tav_got_mode, tav_got_addr;
uvm_cmdline_processor tav_clp = uvm_cmdline_processor::get_inst();
phase.raise_objection(this);
// 1. `ifdef: the choice is made when compiling, the other branch is not in the build
`ifdef TAV_FAST
tav_build = "fast";
`else
tav_build = "slow";
`endif
tav_check("1 `ifdef TAV_FAST", tav_build, "fast");
// 2. A macro with a value can size things, which a plusarg never can
tav_check("2 $bits(tav_bus) from `TAV_WIDTH", $sformatf("%0d", $bits(tav_bus)), "16");
// 3. $test$plusargs: is the switch there?
tav_check("3 $test$plusargs(\"TAV_VERBOSE\")", $sformatf("%0d", $test$plusargs("TAV_VERBOSE")), "1");
// 4. Not given: returns 0, and a missing value keeps the default
tav_got_addr = $value$plusargs("TAV_ADDR=%h", tav_addr);
tav_check("4 TAV_QUIET / TAV_ADDR found / tav_addr",
$sformatf("%0d / %0d / %0h", $test$plusargs("TAV_QUIET"), tav_got_addr, tav_addr), "0 / 0 / 1000");
// 5. $value$plusargs with %d
if (tav_bug == 1)
tav_got_len = $value$plusargs("TAV_LEN=%h", tav_len); // BUG: "12" read as hex
else
tav_got_len = $value$plusargs("TAV_LEN=%d", tav_len);
tav_check("5 +TAV_LEN=12 read with %d", $sformatf("%0d / %0d", tav_got_len, tav_len), "1 / 12");
// 6. $value$plusargs with %s
tav_got_mode = $value$plusargs("TAV_MODE=%s", tav_mode);
tav_check("6 +TAV_MODE=burst read with %s", $sformatf("%0d / %s", tav_got_mode, tav_mode), "1 / burst");
// 7. The prefix trap: $test$plusargs matches the START of a plusarg
tav_check("7 $test$plusargs(\"TAV_LE\") and (\"TAV\")",
$sformatf("%0d / %0d", $test$plusargs("TAV_LE"), $test$plusargs("TAV")), "1 / 1");
// 8. UVM's command-line processor sees the same plusargs
void'(tav_clp.get_arg_value("+TAV_MODE=", tav_mode_uvm));
tav_check("8 uvm_cmdline_processor +TAV_MODE=", tav_mode_uvm, "burst");
`uvm_info("TAV_PLUS", $sformatf("Checks: %0d pass, %0d fail", tav_pass, tav_fail), UVM_NONE)
phase.drop_objection(this);
endtask
endclass
endpackage
module tav_top;
import uvm_pkg::*;
import tav_plus_pkg::*;
initial run_test("tav_plus_test");
endmodule
How to run it
- Open EDA Playground and pick Synopsys VCS with UVM 1.2.
- Paste the file into
testbench.svand leavedesign.svempty. - Compile Options:
+define+TAV_FAST +define+TAV_WIDTH=16 - Run Options:
+TAV_VERBOSE +TAV_LEN=12 +TAV_MODE=burst - Run, then add
+BUG=1to the Run Options and run again.
What VCS returned
| # | Check | Result | What it proves |
|---|---|---|---|
| 1 | `ifdef TAV_FAST | fast | The compiler picked the branch |
| 2 | $bits of a bus sized by `TAV_WIDTH | 16 | A macro value can set a width, a plusarg cannot |
| 3 | $test$plusargs("TAV_VERBOSE") | 1 | The switch was found |
| 4 | Absent TAV_QUIET / missing TAV_ADDR / its value | 0 / 0 / 1000 | Missing means 0, and the default stays |
| 5 | +TAV_LEN=12 with %d | 1 / 12 | Found, and read as decimal |
| 6 | +TAV_MODE=burst with %s | 1 / burst | Strings work too |
| 7 | $test$plusargs("TAV_LE") and ("TAV") | 1 / 1 | The prefix trap: a plusarg that only starts with the name counts |
| 8 | uvm_cmdline_processor, +TAV_MODE= | burst | UVM sees the same command line |
Result: 8 pass, 0 fail. Here is the log.
UVM_INFO @ 0: reporter [RNTST] Running test tav_plus_test...
UVM_INFO testbench.sv(47) @ 0: uvm_test_top [TAV_PLUS] PASS 1 `ifdef TAV_FAST: fast
UVM_INFO testbench.sv(47) @ 0: uvm_test_top [TAV_PLUS] PASS 2 $bits(tav_bus) from `TAV_WIDTH: 16
UVM_INFO testbench.sv(47) @ 0: uvm_test_top [TAV_PLUS] PASS 3 $test$plusargs("TAV_VERBOSE"): 1
UVM_INFO testbench.sv(47) @ 0: uvm_test_top [TAV_PLUS] PASS 4 TAV_QUIET / TAV_ADDR found / tav_addr: 0 / 0 / 1000
UVM_INFO testbench.sv(47) @ 0: uvm_test_top [TAV_PLUS] PASS 5 +TAV_LEN=12 read with %d: 1 / 12
UVM_INFO testbench.sv(47) @ 0: uvm_test_top [TAV_PLUS] PASS 6 +TAV_MODE=burst read with %s: 1 / burst
UVM_INFO testbench.sv(47) @ 0: uvm_test_top [TAV_PLUS] PASS 7 $test$plusargs("TAV_LE") and ("TAV"): 1 / 1
UVM_INFO testbench.sv(47) @ 0: uvm_test_top [TAV_PLUS] PASS 8 uvm_cmdline_processor +TAV_MODE=: burst
UVM_INFO testbench.sv(102) @ 0: uvm_test_top [TAV_PLUS] Checks: 8 pass, 0 fail
UVM_INFO /apps/vcsmx/vcs/X-2025.06-SP1//etc/uvm-1.2/src/base/uvm_objection.svh(1276) @ 0: reporter [TEST_DONE] 'run' phase is ready to proceed to the 'extract' phase
UVM_INFO /apps/vcsmx/vcs/X-2025.06-SP1//etc/uvm-1.2/src/base/uvm_report_server.svh(904) @ 0: reporter [UVM/REPORT/SERVER]
--- UVM Report Summary ---
** Report counts by severity
UVM_INFO : 12
UVM_WARNING : 0
UVM_ERROR : 0
UVM_FATAL : 0
** Report counts by id
[RNTST] 1
[TAV_PLUS] 9
[TEST_DONE] 1
[UVM/RELNOTES] 1
The prefix trap, check 7
$test$plusargs("TAV") returned 1, yet nobody passed +TAV. It matched +TAV_VERBOSE, because $test$plusargs checks whether a plusarg starts with the string you give it.
So a switch named +DEBUG is also “found” when someone passes +DEBUG_LEVEL=2. Give switches names that are not the start of another name.
The planted bug: 12 becomes 18
With +BUG=1, the bench reads +TAV_LEN=12 with %h. The plusarg is found, so the return value is still 1, but the digits are read as hex.
UVM_INFO @ 0: reporter [RNTST] Running test tav_plus_test...
UVM_INFO testbench.sv(47) @ 0: uvm_test_top [TAV_PLUS] PASS 1 `ifdef TAV_FAST: fast
UVM_INFO testbench.sv(47) @ 0: uvm_test_top [TAV_PLUS] PASS 2 $bits(tav_bus) from `TAV_WIDTH: 16
UVM_INFO testbench.sv(47) @ 0: uvm_test_top [TAV_PLUS] PASS 3 $test$plusargs("TAV_VERBOSE"): 1
UVM_INFO testbench.sv(47) @ 0: uvm_test_top [TAV_PLUS] PASS 4 TAV_QUIET / TAV_ADDR found / tav_addr: 0 / 0 / 1000
UVM_ERROR testbench.sv(51) @ 0: uvm_test_top [TAV_PLUS] FAIL 5 +TAV_LEN=12 read with %d: got 1 / 18, expected 1 / 12
UVM_INFO testbench.sv(47) @ 0: uvm_test_top [TAV_PLUS] PASS 6 +TAV_MODE=burst read with %s: 1 / burst
UVM_INFO testbench.sv(47) @ 0: uvm_test_top [TAV_PLUS] PASS 7 $test$plusargs("TAV_LE") and ("TAV"): 1 / 1
UVM_INFO testbench.sv(47) @ 0: uvm_test_top [TAV_PLUS] PASS 8 uvm_cmdline_processor +TAV_MODE=: burst
UVM_INFO testbench.sv(102) @ 0: uvm_test_top [TAV_PLUS] Checks: 7 pass, 1 fail
UVM_INFO /apps/vcsmx/vcs/X-2025.06-SP1//etc/uvm-1.2/src/base/uvm_objection.svh(1276) @ 0: reporter [TEST_DONE] 'run' phase is ready to proceed to the 'extract' phase
UVM_INFO /apps/vcsmx/vcs/X-2025.06-SP1//etc/uvm-1.2/src/base/uvm_report_server.svh(904) @ 0: reporter [UVM/REPORT/SERVER]
--- UVM Report Summary ---
** Report counts by severity
UVM_INFO : 11
UVM_WARNING : 0
UVM_ERROR : 1
UVM_FATAL : 0
** Report counts by id
[RNTST] 1
[TAV_PLUS] 9
[TEST_DONE] 1
[UVM/RELNOTES] 1
Check 5 failed with got 1 / 18. The “found” flag alone would have said everything was fine.
The lesson: after $value$plusargs, print the value you got, not just whether it was found. A wrong format letter gives you a wrong number with no warning.
Using both together
- Default with an override:
`definea default, then let a plusarg change it at run time, as checks 4 and 5 do with their default values. - Heavy debug code: wrap it in
`ifdefso it is not even compiled in normal builds, then use a plusarg to turn it on in debug builds. - UVM built-ins:
+UVM_TESTNAMEand+UVM_VERBOSITYare plusargs, which is why you can pick a test without recompiling.
Common mistakes to avoid
| Mistake | What you see | Fix |
|---|---|---|
Wrong format letter, %h for a decimal | A wrong value, still “found” (+BUG=1) | Match the letter to how the value is written |
| Switch names that share a start | +DEBUG is true when only +DEBUG_LEVEL was given | Use names that are not prefixes of each other |
Putting +define+ in Run Options | The `ifdef stays false | +define+ goes to the compiler |
| Expecting a plusarg to size a bus | Compile error | Use a `define or a parameter |
Not checking the return of $value$plusargs | Silent use of the default | Check it, and report which value is used |
How AI helps you master this
- Sort your switches: paste your list of
`ifdefs and ask which ones could be plusargs, so you recompile less often. - Find prefix clashes: paste your plusarg names and ask which ones are the start of another.
- Write the reader: describe the options a test needs and ask for
$value$plusargslines with defaults and a printout.
Warning: a convincing AI answer is not the same as a correct engineering result. AI tools often say $test$plusargs needs an exact match, but check 7 shows it does not, so run the code.
Interview questions on `ifdef and plusargs
- What is the difference between
`ifdefand plusargs?`ifdefis decided at compile time and can remove code. Plusargs are read at run time, and all code is compiled. - Does changing an
`ifdefneed a recompile? Yes. Changing a plusarg only needs a rerun. - What does
$value$plusargsreturn? Non-zero if the plusarg was found, 0 if not. The value goes into the variable you pass. - Does
$test$plusargs("A")match+AB? Yes. It checks the start of each plusarg. - How does UVM pick a test without recompiling? It reads the
+UVM_TESTNAMEplusarg at run time.
More practice: SystemVerilog interview questions and UVM interview questions.
Practice challenge
- Run the simple example both ways and compare the line numbers.
- Run the full bench, then
+BUG=1, and explain why 12 became 18. - Remove
+define+TAV_WIDTH=16. Predict check 2, then run. - Bonus: add
+TAV_LEN=20a second time on the command line. Which value does$value$plusargsread?
FAQ
What is the difference between ifdef and plusargs? `ifdef is decided by the compiler and can leave code out of the build. Plusargs are read by the running simulation, so the same build can behave differently each run.
Does an ifdef need a recompile? Yes. The decision is part of the build, so a new value means a new compile.
Can a plusarg remove code from the build? No. It can only skip code at run time, and the code is still compiled.
When should I use ifdef instead of a plusarg? For widths, optional blocks that should not be compiled at all, and code that only one tool supports.
When should I use a plusarg instead of ifdef? For anything that changes from test to test, like lengths, modes, seeds and debug switches.
Can I use ifdef and plusargs together? Yes. A common pattern is a compiled-in default that a plusarg can override at run time.
References
- IEEE 1800-2017, SystemVerilog standard: section 22.5 and 22.6 (
`define,`ifdef) and section 21.6 ($test$plusargs,$value$plusargs). - Accellera UVM 1.2 Class Reference:
uvm_cmdline_processor.
Keep learning
- Ending a test cleanly: how to terminate a UVM test.
- Random choices without a class: randcase vs randsequence.
- Interview prep: SystemVerilog interview questions.
Part of the learning path SystemVerilog for verification
