Verilog · Chapter 15.3 · Tasks & Functions
Static vs Automatic Tasks & Functions in Verilog — Storage Behaviour
Tasks and functions have a hidden second setting that decides how their local variables behave across calls. By default they are static, which means one shared set of storage locations used by every call. That is fine when calls never overlap, but the moment a static task or function is called concurrently from parallel processes, or recursively, the calls overwrite each other and produce wrong results. Declaring a task or function automatic instead gives each call its own fresh storage, like a stack frame, so calls stay independent. This lesson explains the difference in plain terms, shows the classic shared-storage bug that static causes, and shows how switching to automatic fixes it. It sets up the reentrancy discussion that follows and helps you choose the right default for reusable code.
Foundation11 min readVerilogstaticautomaticStorageReentrancy
Chapter 15 · Section 15.3 · Tasks & Functions
1. The Engineering Problem
A task or function called from several places at once, or recursively, can produce wrong results — because by default its local storage is shared:
A
static(default) task/function shares one storage location across all calls — so concurrent or recursive calls clobber each other. Anautomatictask/function gives each call its own storage.
This page drills static vs automatic storage.
2. Mental Model — Shared Storage (static) vs Per-Call Storage (automatic)
3. Static Storage — Shared
// static (default) — one shared storage for 'temp':
function [7:0] scaled;
input [7:0] x;
reg [7:0] temp; // ONE shared 'temp' across all calls
begin
temp = x << 1;
scaled = temp + 1;
end
endfunctionWith static storage, every call to scaled uses the same temp. If two calls are active simultaneously (e.g. from two parallel processes), they share temp and clobber each other — the second call's temp overwrites the first's mid-computation. For calls that never overlap (the common single-threaded case), static is fine and efficient.
4. Automatic Storage — Per-Call
// automatic — each call gets its own 'temp':
function automatic [7:0] scaled;
input [7:0] x;
reg [7:0] temp; // FRESH 'temp' per call
begin
temp = x << 1;
scaled = temp + 1;
end
endfunctionDeclaring the function automatic gives each call fresh storage for temp, so concurrent calls are independent — no clobbering. Automatic storage is what makes a task/function reentrant (safe to call concurrently) and recursive (15.4). The cost is per-call allocation, but it is essential when calls can overlap.
Visual A — static vs automatic storage
5. Common Mistakes
- Static task/function called concurrently — shared storage clobbers; use
automatic(§3, DebugLab 1). - Recursion without
automatic— recursion needs per-call storage (§4, 15.4). - Using
automaticeverywhere unnecessarily — static is fine and efficient for non-overlapping calls (§2).
6. Debugging Lab
One static/automatic debug post-mortem
7. Interview Q&A
8. Exercises
Exercise 1 — Static or automatic?
For each, state which storage you need: (a) a function called once per cycle from one block; (b) a recursive function; (c) a task called from three parallel fork threads.
Exercise 2 — Fix the clobber
A static task with a local accumulator is called concurrently and gives wrong results. Give the fix.
Exercise 3 — Explain
Why does a static function's local get clobbered under concurrent calls, but an automatic function's does not?
9. Summary
Static vs automatic governs task/function local storage:
static(default) — one shared storage across all calls; safe only when calls don't overlap.automatic— fresh storage per call; required for recursion and concurrent/reentrant calls.- The bug — a static task/function called concurrently or recursively clobbers its locals.
The next sub-topic builds on this: Chapter 15.4 Re-entrant Tasks & Functions drills automatic for recursion and concurrency — what reentrancy means and when you need it.
Related Tutorials
- Task — Chapter 15.2; the procedures this storage rule applies to.
- Re-entrant Tasks & Functions — Chapter 15.4; recursion and concurrency with
automatic. - Function — Chapter 15.1; the functions this storage rule applies to.
- Sequential & Parallel Blocks — Chapter 14.2;
fork...joinconcurrency that needsautomatic.