CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / tests / amd-exec / section.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """Test-only section generator: copy the actual fixture's dependency closure, not data.""" |
| 4 | import ast |
| 5 | import inspect |
| 6 | import types |
| 7 | |
| 8 | |
| 9 | class Specialize(ast.NodeTransformer): |
| 10 | def __init__(self,name): self.name=name |
| 11 | def choice(self,test): |
| 12 | if (isinstance(test,ast.Compare) and isinstance(test.left,ast.Name) |
| 13 | and test.left.id=='name' and len(test.ops)==1 |
| 14 | and isinstance(test.ops[0],ast.Eq) and isinstance(test.comparators[0],ast.Constant)): |
| 15 | return self.name==test.comparators[0].value |
| 16 | return None |
| 17 | def visit_If(self,node): |
| 18 | node=self.generic_visit(node) |
| 19 | selected=self.choice(node.test) |
| 20 | return node if selected is None else node.body if selected else node.orelse |
| 21 | def visit_IfExp(self,node): |
| 22 | node=self.generic_visit(node) |
| 23 | selected=self.choice(node.test) |
| 24 | return node if selected is None else node.body if selected else node.orelse |
| 25 | |
| 26 | |
| 27 | def generate(name,factory,parameters,controls,runtime): |
| 28 | definitions={}; constants={}; visiting=set() |
| 29 | imports={'base64','struct','Decimal','localcontext','Fraction','lru_cache'} |
| 30 | def visit(symbol,value): |
| 31 | if symbol in imports: return |
| 32 | fn=inspect.unwrap(value) |
| 33 | if not inspect.isfunction(fn): |
| 34 | assert not isinstance(value,types.ModuleType),f'unexpected fixture import {symbol}' |
| 35 | literal=repr(value) |
| 36 | assert symbol not in constants or constants[symbol]==literal,f'constant conflict {symbol}' |
| 37 | constants[symbol]=literal |
| 38 | return |
| 39 | assert fn.__name__==symbol and not fn.__code__.co_freevars,f'fixture closure {symbol}' |
| 40 | tree=ast.parse(inspect.getsource(fn)) |
| 41 | if hasattr(value,'cache_info') and not tree.body[0].decorator_list: |
| 42 | tree.body[0].decorator_list=[ast.parse('lru_cache(None)',mode='eval').body] |
| 43 | text=ast.unparse(tree) |
| 44 | if symbol in definitions: |
| 45 | assert definitions[symbol]==text,f'function conflict {symbol}' |
| 46 | return |
| 47 | assert symbol not in visiting,f'recursive fixture {symbol}' |
| 48 | visiting.add(symbol) |
| 49 | pending=[fn.__code__] |
| 50 | while pending: |
| 51 | code=pending.pop() |
| 52 | for dependency in code.co_names: |
| 53 | if dependency in fn.__globals__: visit(dependency,fn.__globals__[dependency]) |
| 54 | pending.extend(value for value in code.co_consts if isinstance(value,types.CodeType)) |
| 55 | visiting.remove(symbol) |
| 56 | definitions[symbol]=text |
| 57 | visit(factory.__name__,factory) |
| 58 | header='import base64,struct\nfrom decimal import Decimal,localcontext\nfrom fractions import Fraction\nfrom functools import lru_cache\n' |
| 59 | assignments='\n'.join(key+'='+value for key,value in sorted(constants.items())) |
| 60 | create=('def _make_payload():\n' |
| 61 | f' cases=[{factory.__name__}(*args) for args in {tuple(parameters)!r}]\n' |
| 62 | f' return dict(name={name!r},cases=cases,controls={list(controls)!r},timing=len(cases)-1)\n') |
| 63 | runtime=ast.unparse(Specialize(name).visit(ast.parse(runtime))) |
| 64 | return '# Generated from canonical host fixture/oracle functions; no embedded memory images.\n'+header+assignments+'\n'+'\n'.join(definitions.values())+'\n'+create+runtime+'\n' |
| 65 | |
| 66 | |
| 67 | def fresh_namespace(source,name): |
| 68 | """Match the loader's exec(source,{}) and check deferred function lookups too.""" |
| 69 | import builtins |
| 70 | import dis |
| 71 | namespace={} |
| 72 | exec(compile(source,name,'exec'),namespace) |
| 73 | functions=[inspect.unwrap(value) for value in namespace.values()] |
| 74 | pending=[value.__code__ for value in functions if inspect.isfunction(value) and value.__globals__ is namespace] |
| 75 | seen=set() |
| 76 | while pending: |
| 77 | code=pending.pop() |
| 78 | if code in seen: continue |
| 79 | seen.add(code) |
| 80 | for instruction in dis.get_instructions(code): |
| 81 | if instruction.opname=='LOAD_GLOBAL': |
| 82 | symbol=instruction.argval |
| 83 | assert symbol in namespace or hasattr(builtins,symbol), f'unresolved notebook global {symbol} in {code.co_name}' |
| 84 | pending.extend(value for value in code.co_consts if isinstance(value,types.CodeType)) |
| 85 | return namespace |