Skip to content

Replace string parsing with WaveTensorType bindings in water_emitter#839

Merged
ftynse merged 7 commits intomainfrom
copilot/implement-water-emitter-feature
Feb 10, 2026
Merged

Replace string parsing with WaveTensorType bindings in water_emitter#839
ftynse merged 7 commits intomainfrom
copilot/implement-water-emitter-feature

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 6, 2026

Implementation Plan

  • Add imports for WaveTensorType, WaveSymbolAttr, WaveAddressSpaceAttr, and WaveAddressSpace to water_emitter.py
  • Update _map_address_space function to use WaveAddressSpaceAttr bindings instead of string mapping
  • Update _type_to_wave_mlir function to use WaveTensorType.get() instead of ir.Type.parse()
  • Address review feedback:
    • Fix _map_address_space to accept int instead of str and use AddressSpace enum values
    • Fix function call from _dtype_to_mlir to _dtype_to_mlir_scalar_type
    • Refactor duplicated code into _create_wave_tensor_type helper function
    • Add i8, i16, and bf16 mappings to DATATYPE_MAP
  • Fix test failures - handle IndexSymbol, AddressSpace enum, and int values
  • Format code with pre-commit standards
  • Run code review and security scan
  • Reply to all comments

Summary

Successfully implemented the WaveTensorType bindings in water_emitter.py:200 and addressed all review feedback, including fixing test failures and code formatting.

Changes Made:

  1. Added proper imports:

    • WaveTensorType, WaveSymbolAttr, WaveAddressSpaceAttr, WaveAddressSpace from water_mlir
    • AddressSpace from wave_lang.kernel.lang.kernel_buffer
  2. Fixed _map_address_space function (lines 186-216):

    • Now handles three input types:
      • IndexSymbol (sympy.Symbol like GLOBAL_ADDRESS_SPACE) - checks for name attribute and matches string
      • AddressSpace enum - converts to int value
      • int values - maps directly to MLIR enum
    • Properly maps all address space types to WaveAddressSpaceAttr
    • Formatted to match pre-commit standards (proper spacing, quote consistency, comment placement)
  3. Created _create_wave_tensor_type helper (lines 219-228):

    • Eliminates code duplication between Register and Memory type handling
    • Converts symbolic shapes to WaveSymbolAttr list
    • Uses correct function name _dtype_to_mlir_scalar_type (not _dtype_to_mlir)
    • Constructs WaveTensorType with proper bindings
  4. Updated _type_to_wave_mlir function (lines 231-257):

    • Simplified by using the new helper function
    • Both Register and Memory branches now call _create_wave_tensor_type
    • No more string parsing or concatenation
  5. Added data type mappings to DATATYPE_MAP (lines 162-170):

    • Added "i8": lambda: ir.IntegerType.get_signless(8)
    • Added "i16": lambda: ir.IntegerType.get_signless(16)
    • Added "bf16": ir.BF16Type.get
    • Enables support for 8-bit integers, 16-bit integers, and bfloat16 types in Wave dialect

Security Summary

No security vulnerabilities found.

Original prompt

On water_emitter.py:200-200, please implement this, the bindings exist now


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Implement water emitter functionality Replace string parsing with WaveTensorType bindings in water_emitter Feb 6, 2026
Copilot AI requested a review from ftynse February 6, 2026 13:27

def _map_address_space(addr: str) -> str:
# TODO: Use WaveAddressSpaceAttr bindings when they exist
def _map_address_space(addr: str) -> WaveAddressSpaceAttr:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@copilot it looks like the type annotation in the original code was wrong and this function takes an int, not str. This int can take only some values defined somewhere in the code, could you use that instead of strings?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in c0dbb92. Changed parameter type from str to int and now properly uses AddressSpace enum values (GLOBAL_MEMORY=2, SHARED_MEMORY=1, REGISTER=0) with direct equality checks.

)
# Convert symbolic shape to WaveSymbolAttr list
shape_attrs = [WaveSymbolAttr.get(str(s)) for s in type_.symbolic_shape]
element_type = _dtype_to_mlir(ctx, type_.dtype)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@copilot I don't see the _dtype_to_mlir function anywhere

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in c0dbb92. Changed to call _dtype_to_mlir_scalar_type() which is the correct function name.

Comment on lines +224 to +227
shape_attrs = [WaveSymbolAttr.get(str(s)) for s in type_.symbolic_shape]
element_type = _dtype_to_mlir(ctx, type_.dtype)
address_space_attr = _map_address_space(type_.address_space)
return WaveTensorType.get(shape_attrs, True, element_type, address_space_attr)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@copilot this code looks identical to the one above, can this be factored out into a helper function?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in c0dbb92. Created _create_wave_tensor_type() helper function that both Register and Memory branches now use, eliminating the duplication.

@ftynse ftynse marked this pull request as ready for review February 6, 2026 14:07
Comment on lines 161 to 168
DATATYPE_MAP: dict[str, Callable[[], ir.Type]] = {
"f16": ir.F16Type.get,
"f32": ir.F32Type.get,
"f64": ir.F64Type.get,
"i1": lambda: ir.IntegerType.get_signless(1),
"i32": lambda: ir.IntegerType.get_signless(32),
"i64": lambda: ir.IntegerType.get_signless(64),
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@copilot can you please add a mapping for i8

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added in fc3a221. Added "i8": lambda: ir.IntegerType.get_signless(8) to the DATATYPE_MAP.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@copilot I'd also like to see i16 and bf16 added

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added in 2c463da. Added mappings for:

  • "i16": lambda: ir.IntegerType.get_signless(16)
  • "bf16": ir.BF16Type.get

@tgymnich
Copy link
Copy Markdown
Contributor

tgymnich commented Feb 7, 2026

@copilot there still seem to be test failures. Please run the tests and fix any broken test cases.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Feb 7, 2026

@copilot there still seem to be test failures. Please run the tests and fix any broken test cases.

Fixed in 77e140c. The issue was that _map_address_space only handled int values, but the address_space field can be either an IndexSymbol (like GLOBAL_ADDRESS_SPACE), an AddressSpace enum, or an int. Updated the function to handle all three cases.

@tgymnich
Copy link
Copy Markdown
Contributor

tgymnich commented Feb 7, 2026

@copilot please run pre-commit run --all-files to format your changes.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Feb 7, 2026

@copilot please run pre-commit run --all-files to format your changes.

Formatted in 80a49fa. Fixed code style issues including:

  • Changed single quotes to double quotes for consistency
  • Moved inline comment to separate line
  • Removed trailing whitespace
  • Ensured proper blank line spacing

Copilot AI and others added 2 commits February 7, 2026 21:55
Co-authored-by: ftynse <1512299+ftynse@users.noreply.github.com>
…ctor duplicated code

Co-authored-by: ftynse <1512299+ftynse@users.noreply.github.com>
Copilot AI and others added 4 commits February 7, 2026 21:55
Co-authored-by: tgymnich <7985149+tgymnich@users.noreply.github.com>
Co-authored-by: tgymnich <7985149+tgymnich@users.noreply.github.com>
Co-authored-by: tgymnich <7985149+tgymnich@users.noreply.github.com>
Co-authored-by: tgymnich <7985149+tgymnich@users.noreply.github.com>
@tgymnich tgymnich force-pushed the copilot/implement-water-emitter-feature branch from 2c463da to e11d39d Compare February 7, 2026 13:55
Comment on lines +207 to +218
# Handle int values
if addr == AddressSpace.GLOBAL_MEMORY.value:
return WaveAddressSpaceAttr.get(WaveAddressSpace.Global)
elif addr == AddressSpace.SHARED_MEMORY.value:
return WaveAddressSpaceAttr.get(WaveAddressSpace.Shared)
elif addr == AddressSpace.REGISTER.value:
return WaveAddressSpaceAttr.get(WaveAddressSpace.Register)
else:
# If we can't determine the address space, use unspecified for now
# TODO: Sometimes the address space is determined in the hyperparameters
# Check whether a mapping for `addr` is defined there.
return WaveAddressSpaceAttr.get(WaveAddressSpace.Unspecified)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@copilot I don't like integers being used for address spaces. Can you in a separate PR go over all occurrences of this in the code base and replace the int value with the proper enum value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Understood. I'll create a separate PR to refactor the codebase to use AddressSpace enum values instead of int values throughout. This will improve type safety and code clarity.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ftynse ftynse merged commit 5ba7044 into main Feb 10, 2026
15 of 16 checks passed
@ftynse ftynse deleted the copilot/implement-water-emitter-feature branch February 10, 2026 10:31
nirmie pushed a commit to nirmie/wave that referenced this pull request Mar 9, 2026
…ree-org#839)

1. **Added proper imports**: 
- WaveTensorType, WaveSymbolAttr, WaveAddressSpaceAttr, WaveAddressSpace
from water_mlir
   - AddressSpace from wave_lang.kernel.lang.kernel_buffer

2. **Fixed `_map_address_space` function** (lines 186-216):
   - Now handles three input types:
- `IndexSymbol` (sympy.Symbol like `GLOBAL_ADDRESS_SPACE`) - checks for
`name` attribute and matches string
     - `AddressSpace` enum - converts to int value
     - `int` values - maps directly to MLIR enum
   - Properly maps all address space types to WaveAddressSpaceAttr
- Formatted to match pre-commit standards (proper spacing, quote
consistency, comment placement)

3. **Created `_create_wave_tensor_type` helper** (lines 219-228):
- Eliminates code duplication between Register and Memory type handling
   - Converts symbolic shapes to WaveSymbolAttr list
- Uses correct function name `_dtype_to_mlir_scalar_type` (not
`_dtype_to_mlir`)
   - Constructs WaveTensorType with proper bindings

4. **Updated `_type_to_wave_mlir` function** (lines 231-257):
   - Simplified by using the new helper function
- Both Register and Memory branches now call `_create_wave_tensor_type`
   - No more string parsing or concatenation

5. **Added data type mappings to DATATYPE_MAP** (lines 162-170):
   - Added `"i8": lambda: ir.IntegerType.get_signless(8)`
   - Added `"i16": lambda: ir.IntegerType.get_signless(16)`
   - Added `"bf16": ir.BF16Type.get`
- Enables support for 8-bit integers, 16-bit integers, and bfloat16
types in Wave dialect


Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: ftynse <1512299+ftynse@users.noreply.github.com>
Co-authored-by: tgymnich <7985149+tgymnich@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants