-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
ComplexOps.td
567 lines (438 loc) · 16.8 KB
/
ComplexOps.td
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//===- ComplexOps.td - Complex op definitions ----------------*- tablegen -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef COMPLEX_OPS
#define COMPLEX_OPS
include "mlir/Dialect/Complex/IR/ComplexBase.td"
include "mlir/IR/OpAsmInterface.td"
include "mlir/Interfaces/InferTypeOpInterface.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
class Complex_Op<string mnemonic, list<Trait> traits = []>
: Op<Complex_Dialect, mnemonic, traits>;
// Base class for standard arithmetic operations on complex numbers with a
// floating-point element type. These operations take two operands and return
// one result, all of which must be complex numbers of the same type.
class ComplexArithmeticOp<string mnemonic, list<Trait> traits = []> :
Complex_Op<mnemonic, traits # [NoSideEffect, SameOperandsAndResultType,
Elementwise]> {
let arguments = (ins Complex<AnyFloat>:$lhs, Complex<AnyFloat>:$rhs);
let results = (outs Complex<AnyFloat>:$result);
let assemblyFormat = "$lhs `,` $rhs attr-dict `:` type($result)";
}
// Base class for standard unary operations on complex numbers with a
// floating-point element type. These operations take one operand and return
// one result; the operand must be a complex number.
class ComplexUnaryOp<string mnemonic, list<Trait> traits = []> :
Complex_Op<mnemonic, traits # [NoSideEffect, Elementwise]> {
let arguments = (ins Complex<AnyFloat>:$complex);
let assemblyFormat = "$complex attr-dict `:` type($complex)";
}
//===----------------------------------------------------------------------===//
// AbsOp
//===----------------------------------------------------------------------===//
def AbsOp : ComplexUnaryOp<"abs",
[TypesMatchWith<"complex element type matches result type",
"complex", "result",
"$_self.cast<ComplexType>().getElementType()">]> {
let summary = "computes absolute value of a complex number";
let description = [{
The `abs` op takes a single complex number and computes its absolute value.
Example:
```mlir
%a = complex.abs %b : complex<f32>
```
}];
let results = (outs AnyFloat:$result);
}
//===----------------------------------------------------------------------===//
// AddOp
//===----------------------------------------------------------------------===//
def AddOp : ComplexArithmeticOp<"add"> {
let summary = "complex addition";
let description = [{
The `add` operation takes two complex numbers and returns their sum.
Example:
```mlir
%a = complex.add %b, %c : complex<f32>
```
}];
}
//===----------------------------------------------------------------------===//
// Atan2
//===----------------------------------------------------------------------===//
def Atan2Op : ComplexArithmeticOp<"atan2"> {
let summary = "complex 2-argument arctangent";
let description = [{
For complex numbers it is expressed using complex logarithm
atan2(y, x) = -i * log((x + i * y) / sqrt(x**2 + y**2))
Example:
```mlir
%a = complex.atan2 %b, %c : complex<f32>
```
}];
}
//===----------------------------------------------------------------------===//
// ConstantOp
//===----------------------------------------------------------------------===//
def ConstantOp : Complex_Op<"constant", [
ConstantLike, NoSideEffect,
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>
]> {
let summary = "complex number constant operation";
let description = [{
The `complex.constant` operation creates a constant complex number from an
attribute containing the real and imaginary parts.
Example:
```mlir
%a = complex.constant [0.1, -1.0] : complex<f64>
```
}];
let arguments = (ins ArrayAttr:$value);
let results = (outs Complex<AnyFloat>:$complex);
let assemblyFormat = "$value attr-dict `:` type($complex)";
let hasFolder = 1;
let hasVerifier = 1;
let extraClassDeclaration = [{
/// Returns true if a constant operation can be built with the given value
/// and result type.
static bool isBuildableWith(Attribute value, Type type);
}];
}
//===----------------------------------------------------------------------===//
// CosOp
//===----------------------------------------------------------------------===//
def CosOp : ComplexUnaryOp<"cos", [SameOperandsAndResultType]> {
let summary = "computes cosine of a complex number";
let description = [{
The `cos` op takes a single complex number and computes the cosine of
it, i.e. `cos(x)`, where `x` is the input value.
Example:
```mlir
%a = complex.cos %b : complex<f32>
```
}];
let results = (outs Complex<AnyFloat>:$result);
}
//===----------------------------------------------------------------------===//
// CreateOp
//===----------------------------------------------------------------------===//
def CreateOp : Complex_Op<"create",
[NoSideEffect,
AllTypesMatch<["real", "imaginary"]>,
TypesMatchWith<"complex element type matches real operand type",
"complex", "real",
"$_self.cast<ComplexType>().getElementType()">,
TypesMatchWith<"complex element type matches imaginary operand type",
"complex", "imaginary",
"$_self.cast<ComplexType>().getElementType()">]> {
let summary = "complex number creation operation";
let description = [{
The `complex.create` operation creates a complex number from two
floating-point operands, the real and the imaginary part.
Example:
```mlir
%a = complex.create %b, %c : complex<f32>
```
}];
let arguments = (ins AnyFloat:$real, AnyFloat:$imaginary);
let results = (outs Complex<AnyFloat>:$complex);
let assemblyFormat = "$real `,` $imaginary attr-dict `:` type($complex)";
let hasFolder = 1;
}
//===----------------------------------------------------------------------===//
// DivOp
//===----------------------------------------------------------------------===//
def DivOp : ComplexArithmeticOp<"div"> {
let summary = "complex division";
let description = [{
The `div` operation takes two complex numbers and returns result of their
division:
```mlir
%a = complex.div %b, %c : complex<f32>
```
}];
}
//===----------------------------------------------------------------------===//
// EqualOp
//===----------------------------------------------------------------------===//
def EqualOp : Complex_Op<"eq",
[NoSideEffect, AllTypesMatch<["lhs", "rhs"]>, Elementwise]> {
let summary = "computes whether two complex values are equal";
let description = [{
The `eq` op takes two complex numbers and returns whether they are equal.
Example:
```mlir
%a = complex.eq %b, %c : complex<f32>
```
}];
let arguments = (ins Complex<AnyFloat>:$lhs, Complex<AnyFloat>:$rhs);
let results = (outs I1:$result);
let assemblyFormat = "$lhs `,` $rhs attr-dict `:` type($lhs)";
}
//===----------------------------------------------------------------------===//
// ExpOp
//===----------------------------------------------------------------------===//
def ExpOp : ComplexUnaryOp<"exp", [SameOperandsAndResultType]> {
let summary = "computes exponential of a complex number";
let description = [{
The `exp` op takes a single complex number and computes the exponential of
it, i.e. `exp(x)` or `e^(x)`, where `x` is the input value.
`e` denotes Euler's number and is approximately equal to 2.718281.
Example:
```mlir
%a = complex.exp %b : complex<f32>
```
}];
let results = (outs Complex<AnyFloat>:$result);
}
//===----------------------------------------------------------------------===//
// Expm1Op
//===----------------------------------------------------------------------===//
def Expm1Op : ComplexUnaryOp<"expm1", [SameOperandsAndResultType]> {
let summary = "computes exponential of a complex number minus 1";
let description = [{
Syntax:
```
operation ::= ssa-id `=` `complex.expm1` ssa-use `:` type
```
complex.expm1(x) := complex.exp(x) - 1
Example:
```mlir
%a = complex.expm1 %b : complex<f32>
```
}];
let results = (outs Complex<AnyFloat>:$result);
}
//===----------------------------------------------------------------------===//
// ImOp
//===----------------------------------------------------------------------===//
def ImOp : ComplexUnaryOp<"im",
[TypesMatchWith<"complex element type matches result type",
"complex", "imaginary",
"$_self.cast<ComplexType>().getElementType()">]> {
let summary = "extracts the imaginary part of a complex number";
let description = [{
The `im` op takes a single complex number and extracts the imaginary part.
Example:
```mlir
%a = complex.im %b : complex<f32>
```
}];
let results = (outs AnyFloat:$imaginary);
let hasFolder = 1;
}
//===----------------------------------------------------------------------===//
// LogOp
//===----------------------------------------------------------------------===//
def LogOp : ComplexUnaryOp<"log", [SameOperandsAndResultType]> {
let summary = "computes natural logarithm of a complex number";
let description = [{
The `log` op takes a single complex number and computes the natural
logarithm of it, i.e. `log(x)` or `log_e(x)`, where `x` is the input value.
`e` denotes Euler's number and is approximately equal to 2.718281.
Example:
```mlir
%a = complex.log %b : complex<f32>
```
}];
let results = (outs Complex<AnyFloat>:$result);
}
//===----------------------------------------------------------------------===//
// Log1pOp
//===----------------------------------------------------------------------===//
def Log1pOp : ComplexUnaryOp<"log1p", [SameOperandsAndResultType]> {
let summary = "computes natural logarithm of a complex number";
let description = [{
The `log` op takes a single complex number and computes the natural
logarithm of one plus the given value, i.e. `log(1 + x)` or `log_e(1 + x)`,
where `x` is the input value. `e` denotes Euler's number and is
approximately equal to 2.718281.
Example:
```mlir
%a = complex.log1p %b : complex<f32>
```
}];
let results = (outs Complex<AnyFloat>:$result);
}
//===----------------------------------------------------------------------===//
// MulOp
//===----------------------------------------------------------------------===//
def MulOp : ComplexArithmeticOp<"mul"> {
let summary = "complex multiplication";
let description = [{
The `mul` operation takes two complex numbers and returns their product:
```mlir
%a = complex.mul %b, %c : complex<f32>
```
}];
}
//===----------------------------------------------------------------------===//
// NegOp
//===----------------------------------------------------------------------===//
def NegOp : ComplexUnaryOp<"neg", [SameOperandsAndResultType]> {
let summary = "Negation operator";
let description = [{
The `neg` op takes a single complex number `complex` and returns `-complex`.
Example:
```mlir
%a = complex.neg %b : complex<f32>
```
}];
let results = (outs Complex<AnyFloat>:$result);
}
//===----------------------------------------------------------------------===//
// NotEqualOp
//===----------------------------------------------------------------------===//
def NotEqualOp : Complex_Op<"neq",
[NoSideEffect, AllTypesMatch<["lhs", "rhs"]>, Elementwise]> {
let summary = "computes whether two complex values are not equal";
let description = [{
The `neq` op takes two complex numbers and returns whether they are not
equal.
Example:
```mlir
%a = complex.neq %b, %c : complex<f32>
```
}];
let arguments = (ins Complex<AnyFloat>:$lhs, Complex<AnyFloat>:$rhs);
let results = (outs I1:$result);
let assemblyFormat = "$lhs `,` $rhs attr-dict `:` type($lhs)";
}
//===----------------------------------------------------------------------===//
// PowOp
//===----------------------------------------------------------------------===//
def PowOp : ComplexArithmeticOp<"pow"> {
let summary = "complex power function";
let description = [{
The `sqrt` operation takes a complex number raises it to the given complex
exponent.
Example:
```mlir
%a = complex.pow %b, %c : complex<f32>
```
}];
}
//===----------------------------------------------------------------------===//
// ReOp
//===----------------------------------------------------------------------===//
def ReOp : ComplexUnaryOp<"re",
[TypesMatchWith<"complex element type matches result type",
"complex", "real",
"$_self.cast<ComplexType>().getElementType()">]> {
let summary = "extracts the real part of a complex number";
let description = [{
The `re` op takes a single complex number and extracts the real part.
Example:
```mlir
%a = complex.re %b : complex<f32>
```
}];
let results = (outs AnyFloat:$real);
let hasFolder = 1;
}
//===----------------------------------------------------------------------===//
// RsqrtOp
//===----------------------------------------------------------------------===//
def RsqrtOp : ComplexUnaryOp<"rsqrt", [SameOperandsAndResultType]> {
let summary = "complex reciprocal of square root";
let description = [{
The `rsqrt` operation computes reciprocal of square root.
Example:
```mlir
%a = complex.rsqrt %b : complex<f32>
```
}];
let results = (outs Complex<AnyFloat>:$result);
}
//===----------------------------------------------------------------------===//
// SignOp
//===----------------------------------------------------------------------===//
def SignOp : ComplexUnaryOp<"sign", [SameOperandsAndResultType]> {
let summary = "computes sign of a complex number";
let description = [{
The `sign` op takes a single complex number and computes the sign of
it, i.e. `y = sign(x) = x / |x|` if `x != 0`, otherwise `y = 0`.
Example:
```mlir
%a = complex.sign %b : complex<f32>
```
}];
let results = (outs Complex<AnyFloat>:$result);
}
//===----------------------------------------------------------------------===//
// SinOp
//===----------------------------------------------------------------------===//
def SinOp : ComplexUnaryOp<"sin", [SameOperandsAndResultType]> {
let summary = "computes sine of a complex number";
let description = [{
The `sin` op takes a single complex number and computes the sine of
it, i.e. `sin(x)`, where `x` is the input value.
Example:
```mlir
%a = complex.sin %b : complex<f32>
```
}];
let results = (outs Complex<AnyFloat>:$result);
}
//===----------------------------------------------------------------------===//
// SqrtOp
//===----------------------------------------------------------------------===//
def SqrtOp : ComplexUnaryOp<"sqrt", [SameOperandsAndResultType]> {
let summary = "complex square root";
let description = [{
The `sqrt` operation takes a complex number and returns its square root.
Example:
```mlir
%a = complex.sqrt %b : complex<f32>
```
}];
let results = (outs Complex<AnyFloat>:$result);
}
//===----------------------------------------------------------------------===//
// SubOp
//===----------------------------------------------------------------------===//
def SubOp : ComplexArithmeticOp<"sub"> {
let summary = "complex subtraction";
let description = [{
The `sub` operation takes two complex numbers and returns their difference.
Example:
```mlir
%a = complex.sub %b, %c : complex<f32>
```
}];
}
//===----------------------------------------------------------------------===//
// TanhOp
//===----------------------------------------------------------------------===//
def TanhOp : ComplexUnaryOp<"tanh", [SameOperandsAndResultType]> {
let summary = "complex hyperbolic tangent";
let description = [{
The `tanh` operation takes a complex number and returns its hyperbolic
tangent.
Example:
```mlir
%a = complex.tanh %b : complex<f32>
```
}];
let results = (outs Complex<AnyFloat>:$result);
}
//===----------------------------------------------------------------------===//
// TanOp
//===----------------------------------------------------------------------===//
def TanOp : ComplexUnaryOp<"tan", [SameOperandsAndResultType]> {
let summary = "computes tangent of a complex number";
let description = [{
The `tan` op takes a single complex number and computes the tangent of
it, i.e. `tan(x)`, where `x` is the input value.
Example:
```mlir
%a = complex.tan %b : complex<f32>
```
}];
let results = (outs Complex<AnyFloat>:$result);
}
#endif // COMPLEX_OPS