(strandsContext, phiInputs, varName)
| 3 | import { NodeType, BaseType } from './ir_types'; |
| 4 | |
| 5 | export function createPhiNode(strandsContext, phiInputs, varName) { |
| 6 | // Determine the proper dimension and baseType from the inputs |
| 7 | const validInputs = phiInputs.filter(input => input.value.id !== null); |
| 8 | if (validInputs.length === 0) { |
| 9 | throw new Error(`No valid inputs for phi node for variable ${varName}`); |
| 10 | } |
| 11 | |
| 12 | // Get dimension and baseType from first valid input, skipping ASSIGN_ON_USE nodes |
| 13 | const inputNodes = validInputs.map((input) => DAG.getNodeDataFromID(strandsContext.dag, input.value.id)); |
| 14 | |
| 15 | // Find first non-ASSIGN_ON_USE input to determine type |
| 16 | let typeSource = inputNodes.find((input) => input.baseType !== BaseType.ASSIGN_ON_USE && input.dimension) ?? |
| 17 | inputNodes.find((input) => input.baseType !== BaseType.ASSIGN_ON_USE); |
| 18 | |
| 19 | // If all are ASSIGN_ON_USE, fall back to first input |
| 20 | if (!typeSource) { |
| 21 | typeSource = inputNodes[0]; |
| 22 | } |
| 23 | |
| 24 | const dimension = typeSource.dimension; |
| 25 | const baseType = typeSource.baseType; |
| 26 | |
| 27 | // Propagate the type to all ASSIGN_ON_USE inputs |
| 28 | if (baseType !== BaseType.ASSIGN_ON_USE) { |
| 29 | for (const input of inputNodes) { |
| 30 | if (input.baseType === BaseType.ASSIGN_ON_USE) { |
| 31 | DAG.propagateTypeToAssignOnUse(strandsContext.dag, input.id, baseType, dimension); |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | const nodeData = { |
| 37 | nodeType: NodeType.PHI, |
| 38 | dimension, |
| 39 | baseType, |
| 40 | dependsOn: phiInputs.map(input => input.value.id).filter(id => id !== null), |
| 41 | phiBlocks: phiInputs.map(input => input.blockId), |
| 42 | phiInputs // Store the full phi input information |
| 43 | }; |
| 44 | const id = DAG.getOrCreateNode(strandsContext.dag, nodeData); |
| 45 | CFG.recordInBasicBlock(strandsContext.cfg, strandsContext.cfg.currentBlock, id); |
| 46 | return { |
| 47 | id, |
| 48 | dimension, |
| 49 | baseType |
| 50 | }; |
| 51 | } |
no test coverage detected