1 /*
  2  * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package hat.dialect;
 26 
 27 import jdk.incubator.code.CodeContext;
 28 import jdk.incubator.code.CodeTransformer;
 29 import jdk.incubator.code.Op;
 30 import jdk.incubator.code.TypeElement;
 31 import jdk.incubator.code.Value;
 32 import jdk.incubator.code.dialect.core.VarType;
 33 import optkl.util.ops.Precedence;
 34 import optkl.util.ops.StatementLikeOp;
 35 import optkl.util.ops.VarLikeOp;
 36 
 37 import java.util.List;
 38 import java.util.Map;
 39 
 40 
 41 public abstract sealed class  HATF16Op extends HATOp implements VarLikeOp
 42         permits HATF16Op.HATF16BinaryOp, HATF16Op.HATF16ConvOp, HATF16Op.HATF16ToFloatConvOp, HATF16Op.HATF16VarLoadOp, HATF16Op.HATF16VarOp {
 43 
 44 
 45     private String varName;
 46 
 47     public HATF16Op(String varName, List<Value> operands) {
 48         super(operands);
 49         this.varName = varName;
 50     }
 51 
 52     protected HATF16Op(HATF16Op that, CodeContext cc) {
 53         super(that, cc);
 54         this.varName = that.varName;
 55     }
 56     @Override
 57     public String varName() {
 58         return varName;
 59     }
 60 
 61     public void  varName(String varName) {
 62         this.varName = varName;
 63     }
 64 
 65     public static final class HATF16VarOp extends HATF16Op implements StatementLikeOp {
 66 
 67         private final VarType typeElement;
 68         private final ReducedFloatType reducedFloatType;
 69 
 70         public HATF16VarOp(String varName, ReducedFloatType reducedFloatType, VarType typeElement, List<Value> operands) {
 71             super(varName, operands);
 72             this.typeElement = typeElement;
 73             this.reducedFloatType = reducedFloatType;
 74         }
 75 
 76         public HATF16VarOp(HATF16VarOp op, CodeContext copyContext) {
 77             super(op, copyContext);
 78             this.typeElement = op.typeElement;
 79             this.reducedFloatType = op.reducedFloatType;
 80         }
 81 
 82         @Override
 83         public Op transform(CodeContext copyContext, CodeTransformer opTransformer) {
 84             return new HATF16VarOp(this, copyContext);
 85         }
 86 
 87         @Override
 88         public TypeElement resultType() {
 89             return typeElement;
 90         }
 91 
 92         @Override
 93         public Map<String, Object> externalize() {
 94             return Map.of("hat.dialect.fp16varop." + varName(), typeElement);
 95         }
 96 
 97         public ReducedFloatType reducedFloatType() {
 98             return reducedFloatType;
 99         }
100 
101     }
102 
103     public static final class HATF16VarLoadOp extends HATF16Op implements Precedence.LoadOrConv {
104 
105         private final VarType typeElement;
106 
107         public HATF16VarLoadOp(String varName, VarType typeElement, List<Value> operands) {
108             super(varName, operands);
109             this.typeElement = typeElement;
110         }
111 
112         public HATF16VarLoadOp(HATF16VarLoadOp op, CodeContext copyContext) {
113             super(op, copyContext);
114             this.typeElement = op.typeElement;
115         }
116 
117         @Override
118         public Op transform(CodeContext copyContext, CodeTransformer opTransformer) {
119             return new HATF16VarLoadOp(this, copyContext);
120         }
121 
122         @Override
123         public TypeElement resultType() {
124             return typeElement;
125         }
126 
127         @Override
128         public Map<String, Object> externalize() {
129             return Map.of("hat.dialect.fp16VarOp." + varName(), typeElement);
130         }
131 
132     }
133 
134     public static final class HATF16ToFloatConvOp extends HATF16Op implements Precedence.LoadOrConv {
135 
136         private final TypeElement typeElement;
137         private final boolean isLocal;
138         private final boolean wasFloat;
139         private final ReducedFloatType reducedFloatType;
140 
141         public HATF16ToFloatConvOp(TypeElement typeElement, ReducedFloatType reducedFloatType, boolean isLocal, boolean wasFloat, List<Value> operands) {
142             super("", operands);
143             this.typeElement = typeElement;
144             this.isLocal = isLocal;
145             this.wasFloat = wasFloat;
146             this.reducedFloatType = reducedFloatType;
147         }
148 
149         public HATF16ToFloatConvOp(HATF16ToFloatConvOp op, CodeContext copyContext) {
150             super(op, copyContext);
151             this.typeElement = op.typeElement;
152             this.isLocal = op.isLocal;
153             this.wasFloat = op.wasFloat;
154             this.reducedFloatType = op.reducedFloatType;
155         }
156 
157         @Override
158         public Op transform(CodeContext copyContext, CodeTransformer opTransformer) {
159             return new HATF16ToFloatConvOp(this, copyContext);
160         }
161 
162         @Override
163         public TypeElement resultType() {
164             return typeElement;
165         }
166 
167         @Override
168         public Map<String, Object> externalize() {
169             return Map.of("hat.dialect.f16ToFloat", typeElement);
170         }
171 
172         public boolean isLocal() {
173             return isLocal;
174         }
175 
176         public boolean wasFloat() {
177             return wasFloat;
178         }
179 
180         public ReducedFloatType reducedFloatType() {
181             return reducedFloatType;
182         }
183 
184     }
185 
186     public static final class HATF16ConvOp extends HATF16Op {
187 
188         private final TypeElement typeElement;
189         private final ReducedFloatType reducedFloatType;
190 
191         public HATF16ConvOp(TypeElement typeElement, ReducedFloatType reducedFloatType, List<Value> operands) {
192             super("", operands);
193             this.typeElement = typeElement;
194             this.reducedFloatType = reducedFloatType;
195         }
196 
197         public HATF16ConvOp(HATF16ConvOp op, CodeContext copyContext) {
198             super(op, copyContext);
199             this.typeElement = op.typeElement;
200             this.reducedFloatType = op.reducedFloatType;
201         }
202 
203         @Override
204         public Op transform(CodeContext copyContext, CodeTransformer opTransformer) {
205             return new HATF16ConvOp(this, copyContext);
206         }
207 
208         @Override
209         public TypeElement resultType() {
210             return typeElement;
211         }
212 
213         @Override
214         public Map<String, Object> externalize() {
215             return Map.of("hat.dialect.f16Conv", typeElement);
216         }
217 
218         public ReducedFloatType reducedFloatType() {
219             return reducedFloatType;
220         }
221 
222     }
223 
224     public abstract sealed static class HATF16BinaryOp extends HATF16Op
225             permits HATF16BinaryOp.HATF16AddOp, hat.dialect.HATF16Op.HATF16BinaryOp.HATF16DivOp, hat.dialect.HATF16Op.HATF16BinaryOp.HATF16MulOp, hat.dialect.HATF16Op.HATF16BinaryOp.HATF16SubOp {
226 
227         protected final TypeElement elementType;
228         protected final BinaryOpEnum operationType;
229         protected final List<Boolean> references;
230         private final ReducedFloatType reducedFloatType;
231         protected final byte byteFloatRepresentation;
232 
233         public static final byte FIRST_OP = 0x01;
234         public static final byte LAST_OP = 0x10;
235 
236         public HATF16BinaryOp(TypeElement typeElement, ReducedFloatType reducedFloatType, BinaryOpEnum operationType, List<Boolean> references, byte byteFloatRepresentation, List<Value> operands) {
237             super("", operands);
238             this.elementType = typeElement;
239             this.operationType = operationType;
240             this.references = references;
241             this.byteFloatRepresentation = byteFloatRepresentation;
242             this.reducedFloatType = reducedFloatType;
243         }
244 
245         public HATF16BinaryOp(HATF16BinaryOp op, CodeContext copyContext) {
246             super(op, copyContext);
247             this.elementType = op.elementType;
248             this.operationType = op.operationType;
249             this.references = op.references;
250             this.byteFloatRepresentation = op.byteFloatRepresentation;
251             this.reducedFloatType = op.reducedFloatType;
252         }
253 
254         @Override
255         public TypeElement resultType() {
256             return this.elementType;
257         }
258 
259         @Override
260         public Map<String, Object> externalize() {
261             return Map.of("hat.dialect.fp16." + varName(), operationType.symbol());
262         }
263 
264         public BinaryOpEnum binaryOperationType() {
265             return operationType;
266         }
267 
268         public List<Boolean> references() {
269             return references;
270         }
271 
272         public byte getByteFloatRepresentation() {
273             return byteFloatRepresentation;
274         }
275 
276         public ReducedFloatType reducedFloatType() {
277             return reducedFloatType;
278         }
279 
280         public static final class HATF16AddOp extends HATF16BinaryOp implements Precedence.Additive {
281 
282             public HATF16AddOp(TypeElement typeElement, ReducedFloatType reducedFloatType, List<Boolean> references, byte f32, List<Value> operands) {
283                 super(typeElement, reducedFloatType, BinaryOpEnum.ADD, references, f32, operands);
284             }
285 
286             public HATF16AddOp(HATF16AddOp op, CodeContext copyContext) {
287                 super(op, copyContext);
288             }
289 
290             @Override
291             public Op transform(CodeContext copyContext, CodeTransformer opTransformer) {
292                 return new HATF16AddOp(this, copyContext);
293             }
294         }
295 
296         public static final class HATF16DivOp extends HATF16BinaryOp implements Precedence.Multiplicative {
297 
298             public HATF16DivOp(TypeElement typeElement, ReducedFloatType reducedFloatType, List<Boolean> references, byte f32, List<Value> operands) {
299                 super(typeElement, reducedFloatType, BinaryOpEnum.DIV, references, f32, operands);
300             }
301 
302             public HATF16DivOp(HATF16DivOp op, CodeContext copyContext) {
303                 super(op, copyContext);
304             }
305 
306             @Override
307             public Op transform(CodeContext copyContext, CodeTransformer opTransformer) {
308                 return new HATF16DivOp(this, copyContext);
309             }
310         }
311 
312         public static final class HATF16MulOp extends HATF16BinaryOp implements Precedence.Multiplicative {
313 
314             public HATF16MulOp(TypeElement typeElement, ReducedFloatType reducedFloatType, List<Boolean> references, byte f32, List<Value> operands) {
315                 super(typeElement, reducedFloatType, BinaryOpEnum.MUL, references, f32, operands);
316             }
317 
318             public HATF16MulOp(HATF16MulOp op, CodeContext copyContext) {
319                 super(op, copyContext);
320             }
321 
322             @Override
323             public Op transform(CodeContext copyContext, CodeTransformer opTransformer) {
324                 return new HATF16MulOp(this, copyContext);
325             }
326         }
327 
328         public static final class HATF16SubOp extends HATF16BinaryOp implements Precedence.Additive {
329 
330             public HATF16SubOp(TypeElement typeElement, ReducedFloatType reducedFloatType, List<Boolean> references, byte f32, List<Value> operands) {
331                 super(typeElement, reducedFloatType, BinaryOpEnum.SUB, references, f32, operands);
332             }
333 
334             public HATF16SubOp(HATF16SubOp op, CodeContext copyContext) {
335                 super(op, copyContext);
336             }
337 
338             @Override
339             public Op transform(CodeContext copyContext, CodeTransformer opTransformer) {
340                 return new HATF16SubOp(this, copyContext);
341             }
342         }
343     }
344 }