-
Notifications
You must be signed in to change notification settings - Fork 15
fix: make fast-math toggles work #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -165,6 +165,7 @@ def __init__(self) -> None: | |
| self.named_values: dict[str, Any] = {} | ||
| self.function_protos: dict[str, astx.FunctionPrototype] = {} | ||
| self.result_stack: list[ir.Value | ir.Function] = [] | ||
| self._fast_math_enabled = False | ||
|
|
||
| self.initialize() | ||
|
|
||
|
|
@@ -420,7 +421,36 @@ def _emit_fma( | |
| return builder.fma(lhs, rhs, addend, name="vfma") | ||
|
|
||
| fma_fn = self._get_fma_function(lhs.type) | ||
| return builder.call(fma_fn, [lhs, rhs, addend], name="vfma") | ||
| inst = builder.call(fma_fn, [lhs, rhs, addend], name="vfma") | ||
| self._apply_fast_math(inst) | ||
| return inst | ||
|
|
||
| def set_fast_math(self, enabled: bool) -> None: | ||
| """Enable/disable fast-math flags for subsequent FP instructions.""" | ||
| self._fast_math_enabled = enabled | ||
|
|
||
| def _apply_fast_math(self, inst: ir.Instruction) -> None: | ||
| """Attach fast-math flags when enabled and applicable.""" | ||
| if not self._fast_math_enabled: | ||
| return | ||
| ty = inst.type | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback @yuvimittal. and sorry for the delay .Should I handle it with a try-except block?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added defensive handling using getattr and try/except so it won't crash if flags is None or immutable. |
||
| if isinstance(ty, ir.VectorType): | ||
| if not is_fp_type(ty.element): | ||
| return | ||
| elif not is_fp_type(ty): | ||
| return | ||
|
|
||
| flags = getattr(inst, "flags", None) | ||
| if flags is None: | ||
| return | ||
|
|
||
| if "fast" in flags: | ||
| return | ||
|
|
||
omsherikar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try: | ||
| flags.append("fast") | ||
| except (AttributeError, TypeError): | ||
| return | ||
|
|
||
| @dispatch.abstract | ||
| def visit(self, node: astx.AST) -> None: | ||
|
|
@@ -616,6 +646,7 @@ def visit(self, node: astx.BinaryOp) -> None: | |
| result = self._llvm.ir_builder.fadd( | ||
| llvm_lhs, llvm_rhs, name="vfaddtmp" | ||
| ) | ||
| self._apply_fast_math(result) | ||
| else: | ||
| result = self._llvm.ir_builder.add( | ||
| llvm_lhs, llvm_rhs, name="vaddtmp" | ||
|
|
@@ -625,6 +656,7 @@ def visit(self, node: astx.BinaryOp) -> None: | |
| result = self._llvm.ir_builder.fsub( | ||
| llvm_lhs, llvm_rhs, name="vfsubtmp" | ||
| ) | ||
| self._apply_fast_math(result) | ||
| else: | ||
| result = self._llvm.ir_builder.sub( | ||
| llvm_lhs, llvm_rhs, name="vsubtmp" | ||
|
|
@@ -634,6 +666,7 @@ def visit(self, node: astx.BinaryOp) -> None: | |
| result = self._llvm.ir_builder.fmul( | ||
| llvm_lhs, llvm_rhs, name="vfmultmp" | ||
| ) | ||
| self._apply_fast_math(result) | ||
| else: | ||
| result = self._llvm.ir_builder.mul( | ||
| llvm_lhs, llvm_rhs, name="vmultmp" | ||
|
|
@@ -643,6 +676,7 @@ def visit(self, node: astx.BinaryOp) -> None: | |
| result = self._llvm.ir_builder.fdiv( | ||
| llvm_lhs, llvm_rhs, name="vfdivtmp" | ||
| ) | ||
| self._apply_fast_math(result) | ||
| else: | ||
| unsigned = getattr(node, "unsigned", None) | ||
| if unsigned is None: | ||
|
|
@@ -690,6 +724,7 @@ def visit(self, node: astx.BinaryOp) -> None: | |
| result = self._llvm.ir_builder.fadd( | ||
| llvm_lhs, llvm_rhs, "addtmp" | ||
| ) | ||
| self._apply_fast_math(result) | ||
| else: | ||
| # there's more conditions to be handled | ||
| result = self._llvm.ir_builder.add( | ||
|
|
@@ -703,6 +738,7 @@ def visit(self, node: astx.BinaryOp) -> None: | |
| result = self._llvm.ir_builder.fsub( | ||
| llvm_lhs, llvm_rhs, "subtmp" | ||
| ) | ||
| self._apply_fast_math(result) | ||
| else: | ||
| # note: be careful you should handle this as INT32 | ||
| result = self._llvm.ir_builder.sub( | ||
|
|
@@ -717,6 +753,7 @@ def visit(self, node: astx.BinaryOp) -> None: | |
| result = self._llvm.ir_builder.fmul( | ||
| llvm_lhs, llvm_rhs, "multmp" | ||
| ) | ||
| self._apply_fast_math(result) | ||
| else: | ||
| # note: be careful you should handle this as INT32 | ||
| result = self._llvm.ir_builder.mul( | ||
|
|
@@ -782,6 +819,7 @@ def visit(self, node: astx.BinaryOp) -> None: | |
| result = self._llvm.ir_builder.fdiv( | ||
| llvm_lhs, llvm_rhs, "divtmp" | ||
| ) | ||
| self._apply_fast_math(result) | ||
| else: | ||
| # Assuming the division is signed by default. Use `udiv` for | ||
| # unsigned division. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.