1 /*
  2  * Copyright (c) 2025-2026, 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.CodeType;
 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 public abstract sealed class HATF16Op extends HATOp implements VarLikeOp {
 41 
 42     private String varName;
 43 
 44     protected HATF16Op(String varName, List<Value> operands) {
 45         super(operands);
 46         this.varName = varName;
 47     }
 48 
 49     protected HATF16Op(HATF16Op that, CodeContext cc) {
 50         super(that, cc);
 51         this.varName = that.varName;
 52     }
 53     @Override
 54     public String varName() {
 55         return varName;
 56     }
 57 
 58     public void  varName(String varName) {
 59         this.varName = varName;
 60     }
 61 
 62     public static final class HATF16VarLoadOp extends HATF16Op implements Precedence.LoadOrConv {
 63 
 64         private final VarType codeType;
 65 
 66         public HATF16VarLoadOp(String varName, VarType codeType, List<Value> operands) {
 67             super(varName, operands);
 68             this.codeType = codeType;
 69         }
 70 
 71         public HATF16VarLoadOp(HATF16VarLoadOp op, CodeContext copyContext) {
 72             super(op, copyContext);
 73             this.codeType = op.codeType;
 74         }
 75 
 76         @Override
 77         public Op transform(CodeContext copyContext, CodeTransformer opTransformer) {
 78             return new HATF16VarLoadOp(this, copyContext);
 79         }
 80 
 81         @Override
 82         public CodeType resultType() {
 83             return codeType;
 84         }
 85 
 86         @Override
 87         public Map<String, Object> externalize() {
 88             return Map.of("hat.dialect.fp16VarOp." + varName(), codeType);
 89         }
 90 
 91     }
 92 
 93     public static final class HATF16ToFloatConvOp extends HATF16Op implements Precedence.LoadOrConv {
 94 
 95         private final CodeType codeType;
 96         private final boolean isLocal;
 97         private final boolean wasFloat;
 98         private final Class<?> float16Class;
 99 
100         public HATF16ToFloatConvOp(CodeType codeType, Class<?> float16Class, boolean isLocal, boolean wasFloat, List<Value> operands) {
101             super("", operands);
102             this.codeType = codeType;
103             this.isLocal = isLocal;
104             this.wasFloat = wasFloat;
105             this.float16Class = float16Class;
106         }
107 
108         public HATF16ToFloatConvOp(HATF16ToFloatConvOp op, CodeContext copyContext) {
109             super(op, copyContext);
110             this.codeType = op.codeType;
111             this.isLocal = op.isLocal;
112             this.wasFloat = op.wasFloat;
113             this.float16Class = op.float16Class;
114         }
115 
116         @Override
117         public Op transform(CodeContext copyContext, CodeTransformer opTransformer) {
118             return new HATF16ToFloatConvOp(this, copyContext);
119         }
120 
121         @Override
122         public CodeType resultType() {
123             return codeType;
124         }
125 
126         @Override
127         public Map<String, Object> externalize() {
128             return Map.of("hat.dialect.f16ToFloat", codeType);
129         }
130 
131         public boolean isLocal() {
132             return isLocal;
133         }
134 
135         public boolean wasFloat() {
136             return wasFloat;
137         }
138 
139         public Class<?> float16Class() {
140             return float16Class;
141         }
142 
143     }
144 
145     public static final class HATF16ConvOp extends HATF16Op implements Precedence.LoadOrConv {
146 
147         private final CodeType codeType;
148         private final Class<?> float16Class;
149 
150         public HATF16ConvOp(CodeType codeType, Class<?> float16Class, List<Value> operands) {
151             super("", operands);
152             this.codeType = codeType;
153             this.float16Class = float16Class;
154         }
155 
156         public HATF16ConvOp(HATF16ConvOp op, CodeContext copyContext) {
157             super(op, copyContext);
158             this.codeType = op.codeType;
159             this.float16Class = op.float16Class;
160         }
161 
162         @Override
163         public Op transform(CodeContext copyContext, CodeTransformer opTransformer) {
164             return new HATF16ConvOp(this, copyContext);
165         }
166 
167         @Override
168         public CodeType resultType() {
169             return codeType;
170         }
171 
172         @Override
173         public Map<String, Object> externalize() {
174             return Map.of("hat.dialect.f16Conv", codeType);
175         }
176 
177         public Class<?> float16Class() {
178             return float16Class;
179         }
180 
181     }
182 
183     public abstract static sealed class HATF16BinaryOp extends HATF16Op {
184 
185         protected final CodeType codeType;
186         protected final BinaryOpEnum operationType;
187         protected final Class<?> float16Class;
188 
189         public static final byte FIRST_OP = 0x01;
190         public static final byte LAST_OP = 0x10;
191 
192         protected HATF16BinaryOp(CodeType codeType, Class<?> float16Class, BinaryOpEnum operationType, List<Value> operands) {
193             super("", operands);
194             this.codeType = codeType;
195             this.operationType = operationType;
196             this.float16Class = float16Class;
197         }
198 
199         protected HATF16BinaryOp(HATF16BinaryOp op, CodeContext copyContext) {
200             super(op, copyContext);
201             this.codeType = op.codeType;
202             this.operationType = op.operationType;
203             this.float16Class = op.float16Class;
204         }
205 
206         @Override
207         public CodeType resultType() {
208             return this.codeType;
209         }
210 
211         @Override
212         public Map<String, Object> externalize() {
213             return Map.of("hat.dialect.fp16." + varName(), operationType.symbol());
214         }
215 
216         public BinaryOpEnum binaryOperationType() {
217             return operationType;
218         }
219 
220         public Class<?> float16Class() {
221             return float16Class;
222         }
223 
224         public static final class HATF16AddOp extends HATF16BinaryOp implements Precedence.Additive {
225 
226             public HATF16AddOp(CodeType codeType, Class<?> float16Class, List<Value> operands) {
227                 super(codeType, float16Class, BinaryOpEnum.ADD, operands);
228             }
229 
230             public HATF16AddOp(HATF16AddOp op, CodeContext copyContext) {
231                 super(op, copyContext);
232             }
233 
234             @Override
235             public Op transform(CodeContext copyContext, CodeTransformer opTransformer) {
236                 return new HATF16AddOp(this, copyContext);
237             }
238         }
239 
240         public static final class HATF16DivOp extends HATF16BinaryOp implements Precedence.Multiplicative {
241 
242             public HATF16DivOp(CodeType codeType, Class<?> float16Class, List<Value> operands) {
243                 super(codeType, float16Class, BinaryOpEnum.DIV, operands);
244             }
245 
246             public HATF16DivOp(HATF16DivOp op, CodeContext copyContext) {
247                 super(op, copyContext);
248             }
249 
250             @Override
251             public Op transform(CodeContext copyContext, CodeTransformer opTransformer) {
252                 return new HATF16DivOp(this, copyContext);
253             }
254         }
255 
256         public static final class HATF16MulOp extends HATF16BinaryOp implements Precedence.Multiplicative {
257 
258             public HATF16MulOp(CodeType codeType, Class<?> float16Class, List<Value> operands) {
259                 super(codeType, float16Class, BinaryOpEnum.MUL, operands);
260             }
261 
262             public HATF16MulOp(HATF16MulOp op, CodeContext copyContext) {
263                 super(op, copyContext);
264             }
265 
266             @Override
267             public Op transform(CodeContext copyContext, CodeTransformer opTransformer) {
268                 return new HATF16MulOp(this, copyContext);
269             }
270         }
271 
272         public static final class HATF16SubOp extends HATF16BinaryOp implements Precedence.Additive {
273 
274             public HATF16SubOp(CodeType codeType, Class<?> float16Class, List<Value> operands) {
275                 super(codeType, float16Class, BinaryOpEnum.SUB, operands);
276             }
277 
278             public HATF16SubOp(HATF16SubOp op, CodeContext copyContext) {
279                 super(op, copyContext);
280             }
281 
282             @Override
283             public Op transform(CodeContext copyContext, CodeTransformer opTransformer) {
284                 return new HATF16SubOp(this, copyContext);
285             }
286         }
287     }
288 }