CodeGenContext.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. using Ryujinx.Graphics.Shader.StructuredIr;
  2. using Ryujinx.Graphics.Shader.Translation;
  3. using Spv.Generator;
  4. using System;
  5. using System.Collections.Generic;
  6. using static Spv.Specification;
  7. namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
  8. {
  9. using IrConsts = IntermediateRepresentation.IrConsts;
  10. using IrOperandType = IntermediateRepresentation.OperandType;
  11. partial class CodeGenContext : Module
  12. {
  13. private const uint SpirvVersionMajor = 1;
  14. private const uint SpirvVersionMinor = 3;
  15. private const uint SpirvVersionRevision = 0;
  16. private const uint SpirvVersionPacked = (SpirvVersionMajor << 16) | (SpirvVersionMinor << 8) | SpirvVersionRevision;
  17. public StructuredProgramInfo Info { get; }
  18. public ShaderConfig Config { get; }
  19. public int InputVertices { get; }
  20. public Dictionary<int, Instruction> UniformBuffers { get; } = new Dictionary<int, Instruction>();
  21. public Instruction SupportBuffer { get; set; }
  22. public Instruction UniformBuffersArray { get; set; }
  23. public Instruction StorageBuffersArray { get; set; }
  24. public Instruction LocalMemory { get; set; }
  25. public Instruction SharedMemory { get; set; }
  26. public Instruction InputsArray { get; set; }
  27. public Instruction OutputsArray { get; set; }
  28. public Dictionary<TextureMeta, SamplerType> SamplersTypes { get; } = new Dictionary<TextureMeta, SamplerType>();
  29. public Dictionary<TextureMeta, (Instruction, Instruction, Instruction)> Samplers { get; } = new Dictionary<TextureMeta, (Instruction, Instruction, Instruction)>();
  30. public Dictionary<TextureMeta, (Instruction, Instruction)> Images { get; } = new Dictionary<TextureMeta, (Instruction, Instruction)>();
  31. public Dictionary<int, Instruction> Inputs { get; } = new Dictionary<int, Instruction>();
  32. public Dictionary<int, Instruction> Outputs { get; } = new Dictionary<int, Instruction>();
  33. public Dictionary<int, Instruction> InputsPerPatch { get; } = new Dictionary<int, Instruction>();
  34. public Dictionary<int, Instruction> OutputsPerPatch { get; } = new Dictionary<int, Instruction>();
  35. public Instruction CoordTemp { get; set; }
  36. private readonly Dictionary<AstOperand, Instruction> _locals = new Dictionary<AstOperand, Instruction>();
  37. private readonly Dictionary<int, Instruction[]> _localForArgs = new Dictionary<int, Instruction[]>();
  38. private readonly Dictionary<int, Instruction> _funcArgs = new Dictionary<int, Instruction>();
  39. private readonly Dictionary<int, (StructuredFunction, Instruction)> _functions = new Dictionary<int, (StructuredFunction, Instruction)>();
  40. private class BlockState
  41. {
  42. private int _entryCount;
  43. private readonly List<Instruction> _labels = new List<Instruction>();
  44. public Instruction GetNextLabel(CodeGenContext context)
  45. {
  46. return GetLabel(context, _entryCount);
  47. }
  48. public Instruction GetNextLabelAutoIncrement(CodeGenContext context)
  49. {
  50. return GetLabel(context, _entryCount++);
  51. }
  52. public Instruction GetLabel(CodeGenContext context, int index)
  53. {
  54. while (index >= _labels.Count)
  55. {
  56. _labels.Add(context.Label());
  57. }
  58. return _labels[index];
  59. }
  60. }
  61. private readonly Dictionary<AstBlock, BlockState> _labels = new Dictionary<AstBlock, BlockState>();
  62. public Dictionary<AstBlock, (Instruction, Instruction)> LoopTargets { get; set; }
  63. public AstBlock CurrentBlock { get; private set; }
  64. public SpirvDelegates Delegates { get; }
  65. public CodeGenContext(
  66. StructuredProgramInfo info,
  67. ShaderConfig config,
  68. GeneratorPool<Instruction> instPool,
  69. GeneratorPool<LiteralInteger> integerPool) : base(SpirvVersionPacked, instPool, integerPool)
  70. {
  71. Info = info;
  72. Config = config;
  73. if (config.Stage == ShaderStage.Geometry)
  74. {
  75. InputTopology inPrimitive = config.GpuAccessor.QueryPrimitiveTopology();
  76. InputVertices = inPrimitive switch
  77. {
  78. InputTopology.Points => 1,
  79. InputTopology.Lines => 2,
  80. InputTopology.LinesAdjacency => 2,
  81. InputTopology.Triangles => 3,
  82. InputTopology.TrianglesAdjacency => 3,
  83. _ => throw new InvalidOperationException($"Invalid input topology \"{inPrimitive}\".")
  84. };
  85. }
  86. AddCapability(Capability.Shader);
  87. AddCapability(Capability.Float64);
  88. SetMemoryModel(AddressingModel.Logical, MemoryModel.GLSL450);
  89. Delegates = new SpirvDelegates(this);
  90. }
  91. public void StartFunction()
  92. {
  93. _locals.Clear();
  94. _localForArgs.Clear();
  95. _funcArgs.Clear();
  96. }
  97. public void EnterBlock(AstBlock block)
  98. {
  99. CurrentBlock = block;
  100. AddLabel(GetBlockStateLazy(block).GetNextLabelAutoIncrement(this));
  101. }
  102. public Instruction GetFirstLabel(AstBlock block)
  103. {
  104. return GetBlockStateLazy(block).GetLabel(this, 0);
  105. }
  106. public Instruction GetNextLabel(AstBlock block)
  107. {
  108. return GetBlockStateLazy(block).GetNextLabel(this);
  109. }
  110. private BlockState GetBlockStateLazy(AstBlock block)
  111. {
  112. if (!_labels.TryGetValue(block, out var blockState))
  113. {
  114. blockState = new BlockState();
  115. _labels.Add(block, blockState);
  116. }
  117. return blockState;
  118. }
  119. public Instruction NewBlock()
  120. {
  121. var label = Label();
  122. Branch(label);
  123. AddLabel(label);
  124. return label;
  125. }
  126. public Instruction[] GetMainInterface()
  127. {
  128. var mainInterface = new List<Instruction>();
  129. mainInterface.AddRange(Inputs.Values);
  130. mainInterface.AddRange(Outputs.Values);
  131. mainInterface.AddRange(InputsPerPatch.Values);
  132. mainInterface.AddRange(OutputsPerPatch.Values);
  133. if (InputsArray != null)
  134. {
  135. mainInterface.Add(InputsArray);
  136. }
  137. if (OutputsArray != null)
  138. {
  139. mainInterface.Add(OutputsArray);
  140. }
  141. return mainInterface.ToArray();
  142. }
  143. public void DeclareLocal(AstOperand local, Instruction spvLocal)
  144. {
  145. _locals.Add(local, spvLocal);
  146. }
  147. public void DeclareLocalForArgs(int funcIndex, Instruction[] spvLocals)
  148. {
  149. _localForArgs.Add(funcIndex, spvLocals);
  150. }
  151. public void DeclareArgument(int argIndex, Instruction spvLocal)
  152. {
  153. _funcArgs.Add(argIndex, spvLocal);
  154. }
  155. public void DeclareFunction(int funcIndex, StructuredFunction function, Instruction spvFunc)
  156. {
  157. _functions.Add(funcIndex, (function, spvFunc));
  158. }
  159. public Instruction GetFP32(IAstNode node)
  160. {
  161. return Get(AggregateType.FP32, node);
  162. }
  163. public Instruction GetFP64(IAstNode node)
  164. {
  165. return Get(AggregateType.FP64, node);
  166. }
  167. public Instruction GetS32(IAstNode node)
  168. {
  169. return Get(AggregateType.S32, node);
  170. }
  171. public Instruction GetU32(IAstNode node)
  172. {
  173. return Get(AggregateType.U32, node);
  174. }
  175. public Instruction Get(AggregateType type, IAstNode node)
  176. {
  177. if (node is AstOperation operation)
  178. {
  179. var opResult = Instructions.Generate(this, operation);
  180. return BitcastIfNeeded(type, opResult.Type, opResult.Value);
  181. }
  182. else if (node is AstOperand operand)
  183. {
  184. return operand.Type switch
  185. {
  186. IrOperandType.Argument => GetArgument(type, operand),
  187. IrOperandType.Attribute => GetAttribute(type, operand.Value & AttributeConsts.Mask, (operand.Value & AttributeConsts.LoadOutputMask) != 0),
  188. IrOperandType.AttributePerPatch => GetAttributePerPatch(type, operand.Value & AttributeConsts.Mask, (operand.Value & AttributeConsts.LoadOutputMask) != 0),
  189. IrOperandType.Constant => GetConstant(type, operand),
  190. IrOperandType.ConstantBuffer => GetConstantBuffer(type, operand),
  191. IrOperandType.LocalVariable => GetLocal(type, operand),
  192. IrOperandType.Undefined => GetUndefined(type),
  193. _ => throw new ArgumentException($"Invalid operand type \"{operand.Type}\".")
  194. };
  195. }
  196. throw new NotImplementedException(node.GetType().Name);
  197. }
  198. private Instruction GetUndefined(AggregateType type)
  199. {
  200. return type switch
  201. {
  202. AggregateType.Bool => ConstantFalse(TypeBool()),
  203. AggregateType.FP32 => Constant(TypeFP32(), 0f),
  204. AggregateType.FP64 => Constant(TypeFP64(), 0d),
  205. _ => Constant(GetType(type), 0)
  206. };
  207. }
  208. public Instruction GetAttributeElemPointer(int attr, bool isOutAttr, Instruction index, out AggregateType elemType)
  209. {
  210. var storageClass = isOutAttr ? StorageClass.Output : StorageClass.Input;
  211. var attrInfo = AttributeInfo.From(Config, attr, isOutAttr);
  212. int attrOffset = attrInfo.BaseValue;
  213. AggregateType type = attrInfo.Type;
  214. Instruction ioVariable, elemIndex;
  215. Instruction invocationId = null;
  216. if (Config.Stage == ShaderStage.TessellationControl && isOutAttr)
  217. {
  218. invocationId = Load(TypeS32(), Inputs[AttributeConsts.InvocationId]);
  219. }
  220. bool isUserAttr = attr >= AttributeConsts.UserAttributeBase && attr < AttributeConsts.UserAttributeEnd;
  221. if (isUserAttr &&
  222. ((!isOutAttr && Config.UsedFeatures.HasFlag(FeatureFlags.IaIndexing)) ||
  223. (isOutAttr && Config.UsedFeatures.HasFlag(FeatureFlags.OaIndexing))))
  224. {
  225. elemType = AggregateType.FP32;
  226. ioVariable = isOutAttr ? OutputsArray : InputsArray;
  227. elemIndex = Constant(TypeU32(), attrInfo.GetInnermostIndex());
  228. var vecIndex = Constant(TypeU32(), (attr - AttributeConsts.UserAttributeBase) >> 4);
  229. bool isArray = AttributeInfo.IsArrayAttributeSpirv(Config.Stage, isOutAttr);
  230. if (invocationId != null && isArray)
  231. {
  232. return AccessChain(TypePointer(storageClass, GetType(elemType)), ioVariable, invocationId, index, vecIndex, elemIndex);
  233. }
  234. else if (invocationId != null)
  235. {
  236. return AccessChain(TypePointer(storageClass, GetType(elemType)), ioVariable, invocationId, vecIndex, elemIndex);
  237. }
  238. else if (isArray)
  239. {
  240. return AccessChain(TypePointer(storageClass, GetType(elemType)), ioVariable, index, vecIndex, elemIndex);
  241. }
  242. else
  243. {
  244. return AccessChain(TypePointer(storageClass, GetType(elemType)), ioVariable, vecIndex, elemIndex);
  245. }
  246. }
  247. bool isViewportInverse = attr == AttributeConsts.SupportBlockViewInverseX || attr == AttributeConsts.SupportBlockViewInverseY;
  248. if (isViewportInverse)
  249. {
  250. elemType = AggregateType.FP32;
  251. elemIndex = Constant(TypeU32(), (attr - AttributeConsts.SupportBlockViewInverseX) >> 2);
  252. return AccessChain(TypePointer(StorageClass.Uniform, TypeFP32()), SupportBuffer, Constant(TypeU32(), 2), elemIndex);
  253. }
  254. elemType = attrInfo.Type & AggregateType.ElementTypeMask;
  255. if (isUserAttr && Config.TransformFeedbackEnabled &&
  256. ((isOutAttr && Config.LastInVertexPipeline) ||
  257. (!isOutAttr && Config.Stage == ShaderStage.Fragment)))
  258. {
  259. attrOffset = attr;
  260. type = elemType;
  261. if (Config.LastInPipeline && isOutAttr)
  262. {
  263. int components = Info.GetTransformFeedbackOutputComponents(attr);
  264. if (components > 1)
  265. {
  266. attrOffset &= ~0xf;
  267. type = AggregateType.Vector | AggregateType.FP32;
  268. attrInfo = new AttributeInfo(attrOffset, (attr - attrOffset) / 4, components, type, false);
  269. }
  270. }
  271. }
  272. ioVariable = isOutAttr ? Outputs[attrOffset] : Inputs[attrOffset];
  273. bool isIndexed = AttributeInfo.IsArrayAttributeSpirv(Config.Stage, isOutAttr) && (!attrInfo.IsBuiltin || AttributeInfo.IsArrayBuiltIn(attr));
  274. if ((type & (AggregateType.Array | AggregateType.Vector)) == 0)
  275. {
  276. if (invocationId != null)
  277. {
  278. return isIndexed
  279. ? AccessChain(TypePointer(storageClass, GetType(elemType)), ioVariable, invocationId, index)
  280. : AccessChain(TypePointer(storageClass, GetType(elemType)), ioVariable, invocationId);
  281. }
  282. else
  283. {
  284. return isIndexed ? AccessChain(TypePointer(storageClass, GetType(elemType)), ioVariable, index) : ioVariable;
  285. }
  286. }
  287. elemIndex = Constant(TypeU32(), attrInfo.GetInnermostIndex());
  288. if (invocationId != null && isIndexed)
  289. {
  290. return AccessChain(TypePointer(storageClass, GetType(elemType)), ioVariable, invocationId, index, elemIndex);
  291. }
  292. else if (invocationId != null)
  293. {
  294. return AccessChain(TypePointer(storageClass, GetType(elemType)), ioVariable, invocationId, elemIndex);
  295. }
  296. else if (isIndexed)
  297. {
  298. return AccessChain(TypePointer(storageClass, GetType(elemType)), ioVariable, index, elemIndex);
  299. }
  300. else
  301. {
  302. return AccessChain(TypePointer(storageClass, GetType(elemType)), ioVariable, elemIndex);
  303. }
  304. }
  305. public Instruction GetAttributeElemPointer(Instruction attrIndex, bool isOutAttr, Instruction index, out AggregateType elemType)
  306. {
  307. var storageClass = isOutAttr ? StorageClass.Output : StorageClass.Input;
  308. Instruction invocationId = null;
  309. if (Config.Stage == ShaderStage.TessellationControl && isOutAttr)
  310. {
  311. invocationId = Load(TypeS32(), Inputs[AttributeConsts.InvocationId]);
  312. }
  313. elemType = AggregateType.FP32;
  314. var ioVariable = isOutAttr ? OutputsArray : InputsArray;
  315. var vecIndex = ShiftRightLogical(TypeS32(), attrIndex, Constant(TypeS32(), 2));
  316. var elemIndex = BitwiseAnd(TypeS32(), attrIndex, Constant(TypeS32(), 3));
  317. bool isArray = AttributeInfo.IsArrayAttributeSpirv(Config.Stage, isOutAttr);
  318. if (invocationId != null && isArray)
  319. {
  320. return AccessChain(TypePointer(storageClass, GetType(elemType)), ioVariable, invocationId, index, vecIndex, elemIndex);
  321. }
  322. else if (invocationId != null)
  323. {
  324. return AccessChain(TypePointer(storageClass, GetType(elemType)), ioVariable, invocationId, vecIndex, elemIndex);
  325. }
  326. else if (isArray)
  327. {
  328. return AccessChain(TypePointer(storageClass, GetType(elemType)), ioVariable, index, vecIndex, elemIndex);
  329. }
  330. else
  331. {
  332. return AccessChain(TypePointer(storageClass, GetType(elemType)), ioVariable, vecIndex, elemIndex);
  333. }
  334. }
  335. public Instruction GetAttribute(AggregateType type, int attr, bool isOutAttr, Instruction index = null)
  336. {
  337. if (!AttributeInfo.Validate(Config, attr, isOutAttr: false))
  338. {
  339. return GetConstant(type, new AstOperand(IrOperandType.Constant, 0));
  340. }
  341. var elemPointer = GetAttributeElemPointer(attr, isOutAttr, index, out var elemType);
  342. var value = Load(GetType(elemType), elemPointer);
  343. if (Config.Stage == ShaderStage.Fragment)
  344. {
  345. if (attr == AttributeConsts.PositionX || attr == AttributeConsts.PositionY)
  346. {
  347. var pointerType = TypePointer(StorageClass.Uniform, TypeFP32());
  348. var fieldIndex = Constant(TypeU32(), 4);
  349. var scaleIndex = Constant(TypeU32(), 0);
  350. var scaleElemPointer = AccessChain(pointerType, SupportBuffer, fieldIndex, scaleIndex);
  351. var scale = Load(TypeFP32(), scaleElemPointer);
  352. value = FDiv(TypeFP32(), value, scale);
  353. }
  354. else if (attr == AttributeConsts.FrontFacing && Config.GpuAccessor.QueryHostHasFrontFacingBug())
  355. {
  356. // Workaround for what appears to be a bug on Intel compiler.
  357. var valueFloat = Select(TypeFP32(), value, Constant(TypeFP32(), 1f), Constant(TypeFP32(), 0f));
  358. var valueAsInt = Bitcast(TypeS32(), valueFloat);
  359. var valueNegated = SNegate(TypeS32(), valueAsInt);
  360. value = SLessThan(TypeBool(), valueNegated, Constant(TypeS32(), 0));
  361. }
  362. }
  363. return BitcastIfNeeded(type, elemType, value);
  364. }
  365. public Instruction GetAttributePerPatchElemPointer(int attr, bool isOutAttr, out AggregateType elemType)
  366. {
  367. var storageClass = isOutAttr ? StorageClass.Output : StorageClass.Input;
  368. var attrInfo = AttributeInfo.FromPatch(Config, attr, isOutAttr);
  369. int attrOffset = attrInfo.BaseValue;
  370. Instruction ioVariable = isOutAttr ? OutputsPerPatch[attrOffset] : InputsPerPatch[attrOffset];
  371. elemType = attrInfo.Type & AggregateType.ElementTypeMask;
  372. if ((attrInfo.Type & (AggregateType.Array | AggregateType.Vector)) == 0)
  373. {
  374. return ioVariable;
  375. }
  376. var elemIndex = Constant(TypeU32(), attrInfo.GetInnermostIndex());
  377. return AccessChain(TypePointer(storageClass, GetType(elemType)), ioVariable, elemIndex);
  378. }
  379. public Instruction GetAttributePerPatch(AggregateType type, int attr, bool isOutAttr)
  380. {
  381. if (!AttributeInfo.ValidatePerPatch(Config, attr, isOutAttr: false))
  382. {
  383. return GetConstant(type, new AstOperand(IrOperandType.Constant, 0));
  384. }
  385. var elemPointer = GetAttributePerPatchElemPointer(attr, isOutAttr, out var elemType);
  386. return BitcastIfNeeded(type, elemType, Load(GetType(elemType), elemPointer));
  387. }
  388. public Instruction GetAttribute(AggregateType type, Instruction attr, bool isOutAttr, Instruction index = null)
  389. {
  390. var elemPointer = GetAttributeElemPointer(attr, isOutAttr, index, out var elemType);
  391. return BitcastIfNeeded(type, elemType, Load(GetType(elemType), elemPointer));
  392. }
  393. public Instruction GetConstant(AggregateType type, AstOperand operand)
  394. {
  395. return type switch
  396. {
  397. AggregateType.Bool => operand.Value != 0 ? ConstantTrue(TypeBool()) : ConstantFalse(TypeBool()),
  398. AggregateType.FP32 => Constant(TypeFP32(), BitConverter.Int32BitsToSingle(operand.Value)),
  399. AggregateType.FP64 => Constant(TypeFP64(), (double)BitConverter.Int32BitsToSingle(operand.Value)),
  400. AggregateType.S32 => Constant(TypeS32(), operand.Value),
  401. AggregateType.U32 => Constant(TypeU32(), (uint)operand.Value),
  402. _ => throw new ArgumentException($"Invalid type \"{type}\".")
  403. };
  404. }
  405. public Instruction GetConstantBuffer(AggregateType type, AstOperand operand)
  406. {
  407. var i1 = Constant(TypeS32(), 0);
  408. var i2 = Constant(TypeS32(), operand.CbufOffset >> 2);
  409. var i3 = Constant(TypeU32(), operand.CbufOffset & 3);
  410. Instruction elemPointer;
  411. if (UniformBuffersArray != null)
  412. {
  413. var ubVariable = UniformBuffersArray;
  414. var i0 = Constant(TypeS32(), operand.CbufSlot);
  415. elemPointer = AccessChain(TypePointer(StorageClass.Uniform, TypeFP32()), ubVariable, i0, i1, i2, i3);
  416. }
  417. else
  418. {
  419. var ubVariable = UniformBuffers[operand.CbufSlot];
  420. elemPointer = AccessChain(TypePointer(StorageClass.Uniform, TypeFP32()), ubVariable, i1, i2, i3);
  421. }
  422. return BitcastIfNeeded(type, AggregateType.FP32, Load(TypeFP32(), elemPointer));
  423. }
  424. public Instruction GetLocalPointer(AstOperand local)
  425. {
  426. return _locals[local];
  427. }
  428. public Instruction[] GetLocalForArgsPointers(int funcIndex)
  429. {
  430. return _localForArgs[funcIndex];
  431. }
  432. public Instruction GetArgumentPointer(AstOperand funcArg)
  433. {
  434. return _funcArgs[funcArg.Value];
  435. }
  436. public Instruction GetLocal(AggregateType dstType, AstOperand local)
  437. {
  438. var srcType = local.VarType.Convert();
  439. return BitcastIfNeeded(dstType, srcType, Load(GetType(srcType), GetLocalPointer(local)));
  440. }
  441. public Instruction GetArgument(AggregateType dstType, AstOperand funcArg)
  442. {
  443. var srcType = funcArg.VarType.Convert();
  444. return BitcastIfNeeded(dstType, srcType, Load(GetType(srcType), GetArgumentPointer(funcArg)));
  445. }
  446. public (StructuredFunction, Instruction) GetFunction(int funcIndex)
  447. {
  448. return _functions[funcIndex];
  449. }
  450. public Instruction GetType(AggregateType type, int length = 1)
  451. {
  452. if (type.HasFlag(AggregateType.Array))
  453. {
  454. return TypeArray(GetType(type & ~AggregateType.Array), Constant(TypeU32(), length));
  455. }
  456. else if (type.HasFlag(AggregateType.Vector))
  457. {
  458. return TypeVector(GetType(type & ~AggregateType.Vector), length);
  459. }
  460. return type switch
  461. {
  462. AggregateType.Void => TypeVoid(),
  463. AggregateType.Bool => TypeBool(),
  464. AggregateType.FP32 => TypeFP32(),
  465. AggregateType.FP64 => TypeFP64(),
  466. AggregateType.S32 => TypeS32(),
  467. AggregateType.U32 => TypeU32(),
  468. _ => throw new ArgumentException($"Invalid attribute type \"{type}\".")
  469. };
  470. }
  471. public Instruction BitcastIfNeeded(AggregateType dstType, AggregateType srcType, Instruction value)
  472. {
  473. if (dstType == srcType)
  474. {
  475. return value;
  476. }
  477. if (dstType == AggregateType.Bool)
  478. {
  479. return INotEqual(TypeBool(), BitcastIfNeeded(AggregateType.S32, srcType, value), Constant(TypeS32(), 0));
  480. }
  481. else if (srcType == AggregateType.Bool)
  482. {
  483. var intTrue = Constant(TypeS32(), IrConsts.True);
  484. var intFalse = Constant(TypeS32(), IrConsts.False);
  485. return BitcastIfNeeded(dstType, AggregateType.S32, Select(TypeS32(), value, intTrue, intFalse));
  486. }
  487. else
  488. {
  489. return Bitcast(GetType(dstType, 1), value);
  490. }
  491. }
  492. public Instruction TypeS32()
  493. {
  494. return TypeInt(32, true);
  495. }
  496. public Instruction TypeU32()
  497. {
  498. return TypeInt(32, false);
  499. }
  500. public Instruction TypeFP32()
  501. {
  502. return TypeFloat(32);
  503. }
  504. public Instruction TypeFP64()
  505. {
  506. return TypeFloat(64);
  507. }
  508. }
  509. }