Module 5: Configuration and Extension | Intermediate UVM

Factory Instance Overrides

Replace the implementation of one specific UVM hierarchy instance while leaving other instances of the same base type unchanged.

Learning objectives

  • Explain why instance-specific factory overrides are needed.
  • Distinguish a type override from an instance override.
  • Use set_inst_override_by_type() to replace a specific factory-created instance.
  • Understand how hierarchical instance paths are used for factory matching.
  • Use the correct full instance path when installing an override.
  • Verify that one targeted component was replaced while other instances remained unchanged.
  • Understand override timing and the requirement for factory-based construction.
  • Debug instance overrides using topology, factory diagnostics, and actual type reporting.
  • Recognize when configuration is preferable to a factory override.
  • Prepare for Lesson 46 by identifying common sources of factory override conflicts.

Step 1: Start with the problem

Lesson 44 introduced the factory type override. A type override is useful when every matching request for a type should use the replacement.

But consider a reusable environment containing three agents:

uvm_test_top


|
+-- env
|
+-- command_agent
|    +-- driver
|
+-- response_agent
|    +-- driver
|
+-- monitor_agent
+-- driver

Suppose only the command driver should inject malformed transfers:

command_agent.driver  -> error_injecting_driver


response_agent.driver -> protocol_driver
monitor_agent.driver  -> protocol_driver

A type override is too broad:

protocol_driver
   |
   +-- type override
          |
          v
   error_injecting_driver


Result:
command_agent  -> error driver
response_agent -> error driver
monitor_agent  -> error driver

An instance override lets the test target one hierarchy instance.

protocol_driver
   |
   +-- instance override
          |
          v
   env.command_agent.driver


Only this instance becomes:
error_injecting_driver
Core idea: A type override changes the implementation for matching type requests broadly. An instance override narrows the substitution to a particular factory-created hierarchy instance.

Step 2: Compare type and instance overrides

Requirement Mechanism Typical result
Replace all protocol drivers Type override Every matching driver request is replaced
Replace only command_agent's driver Instance override Only the targeted hierarchy instance is replaced
Change timeout for one agent Configuration Same implementation, different policy
Change VIF for one agent Configuration Same implementation, different resource
Replace a component implementation Factory override Different class is constructed

The mechanism should match the scope of the intended change.

Step 3: Make the reusable component factory-aware

As in Lesson 44, the component must be created through the factory.

class protocol_agent extends uvm_agent;


`uvm_component_utils(protocol_agent)

protocol_driver driver;

function new(
string name = "protocol_agent",
uvm_component parent = null
);
super.new(name, parent);
endfunction

function void build_phase(uvm_phase phase);
super.build_phase(phase);


if (is_active == UVM_ACTIVE) begin
  driver = protocol_driver::type_id::create(
    "driver",
    this
  );
end


endfunction

endclass

The agent requests protocol_driver. It does not need to know whether the factory will return the normal driver or a specialized replacement.

Step 4: Create the replacement type

class error_injecting_driver extends protocol_driver;


`uvm_component_utils(error_injecting_driver)

function new(
string name = "error_injecting_driver",
uvm_component parent = null
);
super.new(name, parent);
endfunction

endclass

The replacement derives from protocol_driver, so the agent's base-class handle remains valid.

protocol_driver driver;

            |
            v


actual object:
error_injecting_driver

This is the same substitutability principle used by type overrides.

Step 5: Identify the exact instance path

Instance overrides depend on hierarchy.

Suppose the topology is:

uvm_test_top


|
+-- env
|
+-- command_agent
|
+-- driver

The full name of the driver is:

uvm_test_top.env.command_agent.driver

The factory instance override needs a path that identifies the target instance.

Step 6: Install an instance override

The factory provides an instance-specific API:

uvm_factory::get().set_inst_override_by_type(
protocol_driver::get_type(),
error_injecting_driver::get_type(),
"uvm_test_top.env.command_agent.driver"


);

The three important arguments are:

requested type
  |
  v


protocol_driver

replacement type
|
v
error_injecting_driver

target instance
|
v
uvm_test_top.env.command_agent.driver

Conceptually, the factory now knows:

When this particular instance requests:
protocol_driver


create:
error_injecting_driver

Step 7: Install the override from the test

The test is usually the appropriate place for test-specific implementation substitution.

class targeted_error_test extends uvm_test;


`uvm_component_utils(targeted_error_test)

function new(
string name = "targeted_error_test",
uvm_component parent = null
);
super.new(name, parent);
endfunction

function void build_phase(uvm_phase phase);


uvm_factory::get().set_inst_override_by_type(
  protocol_driver::get_type(),
  error_injecting_driver::get_type(),
  "uvm_test_top.env.command_agent.driver"
);

super.build_phase(phase);


endfunction

endclass

The important architectural separation is:

Reusable environment:
creates protocol_driver through factory


Test:
decides that one instance should be specialized

Factory:
performs the substitution

Step 8: Understand why the path matters

Consider two drivers:

uvm_test_top.env.command_agent.driver


uvm_test_top.env.response_agent.driver

An instance override targeting:

uvm_test_top.env.command_agent.driver

should not be interpreted as a request to replace:

uvm_test_top.env.response_agent.driver

The hierarchy is what makes the two otherwise identical component types distinguishable.

Same type


|
+-- command_agent.driver
|
+-- response_agent.driver

Different instance paths
|
v
Different factory matching scope

Step 9: Verify that only one instance changed

After construction, report the actual type of each driver.

`uvm_info(


"FACTORY",
$sformatf(
"%s -> %s",
command_agent.driver.get_full_name(),
command_agent.driver.get_type_name()
),
UVM_MEDIUM
)

`uvm_info(
"FACTORY",
$sformatf(
"%s -> %s",
response_agent.driver.get_full_name(),
response_agent.driver.get_type_name()
),
UVM_MEDIUM
)

Expected result:

uvm_test_top.env.command_agent.driver
-> error_injecting_driver


uvm_test_top.env.response_agent.driver
-> protocol_driver

This demonstrates the central property of an instance override: the replacement is selective.

Step 10: Understand factory creation timing

The override must exist before the affected component is constructed.

Install override
   |
   v


Environment build
|
v
agent creates driver
|
v
factory resolves instance
|
v
replacement is constructed

If the driver already exists, installing the override does not retroactively change its class.

For example:

build_phase:
create driver


run_phase:
install instance override

is too late for that driver.

Step 11: Understand factory-based construction

Just like type overrides, instance overrides cannot affect direct construction.

This is factory-controlled:

driver = protocol_driver::type_id::create(
"driver",
this


);

This bypasses the factory:

driver = new("driver", this);

If the environment uses new, the factory has no opportunity to perform the substitution.

Step 12: Use topology output to find the target path

When the path is uncertain:

uvm_top.print_topology();

Use the resulting hierarchy to determine the actual component names.

For example, the environment may create:

env.command_agent.driver

while the test author incorrectly assumes:

env.cmd_agent.driver

The override will not match simply because the intended meaning is obvious to the engineer.

Step 13: Understand instance paths as factory matching patterns

The instance path supplied to the factory is used to determine whether the override applies to the requested creation.

For a single target, use a precise path:

"uvm_test_top.env.command_agent.driver"

The factory also supports instance matching patterns, allowing a group of instances to be targeted when that is intentionally required.

However, broader matching increases the scope of the override.

Exact target:
env.command_agent.driver


Broader target:
multiple matching driver instances

Use the narrowest matching scope that expresses the test's intent clearly.

Step 14: Understand precedence between type and instance overrides

A real environment can contain both a type override and an instance override.

Type override:
protocol_driver -> debug_driver


Instance override:
env.command_agent.driver
-> error_injecting_driver

The factory has more specific information for the targeted instance.

This creates an important distinction:

Most drivers:
protocol_driver -> debug_driver


One selected instance:
command_agent.driver
-> error_injecting_driver

Factory precedence and matching rules determine which applicable override is selected. Do not assume that the order of arbitrary source-code statements alone explains the result.

Important: When multiple factory overrides are present, inspect the factory rather than guessing from the order in which statements appear in the test.

Step 15: Know when configuration is better

Suppose the only difference between two drivers is a timeout:

command_driver:
timeout = 100


response_driver:
timeout = 500

Do not create two driver subclasses just to hold different numbers.

Prefer configuration:

protocol_driver
  |
  +-- command_cfg.timeout = 100
  |
  +-- response_cfg.timeout = 500

Use an instance override when the implementation itself needs to change:

command_agent.driver:
error_injecting_driver


response_agent.driver:
protocol_driver

Step 16: Keep the replacement compatible

The instance override changes the actual object type, but the consuming component still expects the requested base type.

protocol_driver driver;

Therefore:

class error_injecting_driver extends protocol_driver;




        

is a sensible replacement relationship.

A completely unrelated class is not a valid substitute merely because it has a similarly named method.

Step 17: Debug an instance override that did not take effect

Use a systematic sequence:

Override did not take effect
        |
        v
1. Is the base type registered?
        |
        v
2. Is the replacement registered?
        |
        v
3. Is the replacement compatible?
        |
        v
4. Does the component use type_id::create()?
        |
        v
5. Was the override installed before creation?
        |
        v
6. Is the instance path correct?
        |
        v
7. Does the path match the actual hierarchy?
        |
        v
8. Are other factory overrides also active?
        |
        v
9. What does uvm_factory::print() show?
        |
        v
10. What type was actually constructed?

Step 18: Inspect the factory

When several overrides exist, inspect the factory:

uvm_factory::get().print();

Look for:

registered base type
registered replacement type
type overrides
instance overrides
target instance patterns

This is particularly important when a large testbench has accumulated overrides across base tests and derived tests.

Step 19: Avoid unnecessary instance overrides

Instance overrides are useful, but they introduce hierarchy coupling.

A test containing:

"uvm_test_top.env.agent_a.subsystem_0.driver_3"

knows a great deal about the environment's internal structure.

If that hierarchy changes, the override path may need to change too.

Therefore:

Use type override when:
    every relevant instance should change.

Use instance override when:
    one or a deliberately selected group
    must change.

Use configuration when:
    only policy/settings differ.

Common mistakes

Try it yourself: one special driver

Create an environment with:

uvm_test_top.env
    |
    +-- agent_a.driver
    |
    +-- agent_b.driver

Both drivers should normally be protocol_driver.

Create:

debug_driver extends protocol_driver

Install an instance override so that only:

agent_b.driver

becomes debug_driver.

Verify:

agent_a.driver -> protocol_driver
agent_b.driver -> debug_driver

Debugging exercise: the wrong driver changed

The test intends to replace:

uvm_test_top.env.command_agent.driver

but both command and response drivers become error_injecting_driver.

Investigate whether the test accidentally installed:

set_type_override_by_type()

instead of:

set_inst_override_by_type()

Then inspect the factory and actual component types.

Goal: explain not only how to fix the result, but why the original override had broader scope than intended.

Challenge: selective error injection

Build a reusable environment containing:

protocol_env
    |
    +-- master_agent
    |    +-- driver
    |
    +-- slave_agent
         +-- driver

Implement:

  1. A normal protocol_driver.
  2. An error_injecting_driver derived from it.
  3. Factory registration for both.
  4. Factory-based driver creation in the reusable agent.
  5. A test-specific instance override targeting only slave_agent.driver.
  6. Diagnostics showing both actual driver types.

Then add a second test that uses a type override instead.

Compare the results:

Test A:
    master -> normal
    slave  -> error

Test B:
    master -> error
    slave  -> error

Explain why the two tests use different factory mechanisms.

Advanced extension: Add three agents and intentionally create both a type override and an instance override. Use factory diagnostics to determine which implementation each instance receives.

Practical decision checklist

  1. Do I need to change the implementation or merely a setting?
  2. Should every instance of the requested type change?
  3. If not, which exact hierarchy instance should change?
  4. Is the target created through type_id::create()?
  5. Are the base and replacement types registered?
  6. Is the replacement compatible with the requested type?
  7. Is the instance path based on the actual UVM topology?
  8. Is the override installed before the affected object is created?
  9. Could another factory override also match?
  10. Have I verified the actual constructed type?

Knowledge check

  1. What is the main difference between a type override and an instance override?
    A type override broadly replaces a requested type, while an instance override targets a particular hierarchy instance or matching instance scope.
  2. Which API installs an instance type override?
    set_inst_override_by_type().
  3. Why does the target instance path matter?
    The factory uses the hierarchy path to determine whether the instance override applies to the creation request.
  4. Can an instance override affect an object already constructed?
    No. The override must be established before the affected factory creation occurs.
  5. Why does type_id::create() matter?
    It sends the creation request through the factory.
  6. Why might configuration be better than an instance override?
    If only policy or settings differ, configuration preserves the same implementation instead of introducing unnecessary type substitution.
  7. How do you find a questionable instance path?
    Inspect the actual UVM topology, for example with uvm_top.print_topology().
  8. How can you verify that an override worked?
    Report the actual constructed type with methods such as get_type_name() and inspect the factory when needed.
  9. Why should instance overrides be used carefully?
    They couple the test to hierarchy structure and can become fragile when reusable environment topology changes.

Key takeaways

  • Instance overrides let a test replace the implementation of a specific factory-created hierarchy instance.
  • set_inst_override_by_type() expresses instance-specific type substitution.
  • The target path must correspond to the actual UVM hierarchy.
  • The affected component must be created through the factory for the override to apply.
  • The override must be installed before the affected component or object is constructed.
  • The replacement should be compatible with the requested base type.
  • Use a type override when broad substitution is intended; use an instance override when only selected instances should change.
  • Use configuration when the implementation stays the same and only settings or policy differ.
  • Verify the actual constructed type rather than assuming the override worked.
  • Instance overrides are powerful but introduce hierarchy coupling, so use the narrowest intentional scope.

What comes next?

The factory sequence now has a clear progression:

Lesson 43
Configuration Scope and Precedence
        |
        v
Lesson 44
Factory Type Overrides
        |
        v
Lesson 45
Factory Instance Overrides
        |
        v
Lesson 46
Factory Debugging

Lesson 44 taught:

"Replace the implementation type broadly."

Lesson 45 taught:

"Replace only the implementation at this hierarchy instance."

But real environments can contain multiple type overrides, instance overrides, inherited tests, and complicated factory registrations.

Lesson 46: Factory Debugging will turn those mechanisms into a systematic debugging workflow: inspect the factory, identify matching overrides, verify construction paths, diagnose unexpected replacements, and determine why the factory selected the implementation you actually received.