pliron 0.14.0

Programming Languages Intermediate RepresentatiON
Documentation
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
use std::cmp::Ordering;

use crate::attribute::Attribute;
use crate::context::{Context, Ptr};
use crate::result::Result;
use crate::r#type::TypeObj;
use crate::utils::apfloat::{Category, DynFloat, ExpInt, Round, Semantics, StatusAnd};
use pliron::derive::attr_interface;

/// [Attribute]s that have an associated [Type](crate::type::Type).
/// This serves the same purpose as MLIR's `TypedAttrInterface`.
#[attr_interface]
pub trait TypedAttrInterface {
    /// Get this attribute's type.
    fn get_type(&self, ctx: &Context) -> Ptr<TypeObj>;

    fn verify(_attr: &dyn Attribute, _ctx: &Context) -> Result<()>
    where
        Self: Sized,
    {
        Ok(())
    }
}

/// [Attribute]s that should be printed after the top level [Operation](crate::operation::Operation)
/// is printed. An [Op](crate::op::Op) may choose to print such an attribute as part of its
/// syntax specification. This will be unknown to the outline attributes printer and will be
/// printed nevertheless while printing all outline attributes.
#[attr_interface]
pub trait OutlinedAttr {
    fn verify(_attr: &dyn Attribute, _ctx: &Context) -> Result<()>
    where
        Self: Sized,
    {
        Ok(())
    }
}

/// [Attribute]s that should be printed only once, and some form of "reference" to be
/// used for repeated printing. These must be outlined attributes.
#[attr_interface]
pub trait PrintOnceAttr: OutlinedAttr {
    fn verify(_attr: &dyn Attribute, _ctx: &Context) -> Result<()>
    where
        Self: Sized,
    {
        Ok(())
    }
}

/// [Attribute]s that represent a floating point value.
///
/// This trait provides operations on floating point values
/// from the [DynFloat] trait.
///
/// The `build_*` methods can be used to construct a new [FloatAttr]
/// with the same type as the argument. This can be useful when doing
/// folding optimizations, where we have some dyn instance and want to build
/// another one of the same type but with a different value.
/// The `self` argument is ignored in these methods. In the absense of
/// of an object (but when the concrete type is known), then the static (`Self: Sized`)
/// methods on the [Float](rustc_apfloat::Float) trait can be used instead.
#[attr_interface]
pub trait FloatAttr: TypedAttrInterface {
    /// Get the underlying floating point value as a [DynFloat] trait object.
    fn get_inner(&self) -> &dyn DynFloat;
    /// Build a [FloatAttr] with the same concrete type as `Self`, from the given trait object.
    fn build_from(&self, df: Box<dyn DynFloat>) -> Box<dyn FloatAttr>;
    /// Get semantics of the underlying floating point type.
    fn get_semantics(&self) -> Semantics;
    /// Static version of `get_semantics`.
    fn get_semantics_static() -> Semantics
    where
        Self: Sized;

    /// [Float::qnan](rustc_apfloat::Float::qnan), `self` is ignored
    fn build_qnan(&self, payload: Option<u128>) -> Box<dyn FloatAttr> {
        let df = self.get_inner();
        let qnan = df.build_qnan(payload);
        self.build_from(qnan)
    }
    /// [Float::snan](rustc_apfloat::Float::snan), `self` is ignored
    fn build_snan(&self, payload: Option<u128>) -> Box<dyn FloatAttr> {
        let df = self.get_inner();
        let snan = df.build_snan(payload);
        self.build_from(snan)
    }
    /// [Float::largest](rustc_apfloat::Float::largest), `self` is ignored
    fn build_largest(&self) -> Box<dyn FloatAttr> {
        let df = self.get_inner();
        let largest = df.build_largest();
        self.build_from(largest)
    }
    /// [Float::smallest_normalized](rustc_apfloat::Float::smallest_normalized), `self` is ignored
    fn build_smallest_normalized(&self) -> Box<dyn FloatAttr> {
        let df = self.get_inner();
        let smallest_normalized = df.build_smallest_normalized();
        self.build_from(smallest_normalized)
    }
    /// [Float::from_bits](rustc_apfloat::Float::from_bits), `self` is ignored
    fn build_from_bits(&self, bits: u128) -> Box<dyn FloatAttr> {
        let df = self.get_inner();
        let from_bits = df.build_from_bits(bits);
        self.build_from(from_bits)
    }
    /// [Float::from_u128_r](rustc_apfloat::Float::from_u128_r), `self` is ignored
    fn build_from_u128_r(&self, value: u128, round: Round) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        df.build_from_u128_r(value, round)
            .map(|df| self.build_from(df))
    }
    /// [Float::from_str_r](rustc_apfloat::Float::from_str_r), `self` is ignored
    fn build_from_str_r(&self, s: &str, round: Round) -> Result<StatusAnd<Box<dyn FloatAttr>>> {
        let df = self.get_inner();
        let res = df.build_from_str_r(s, round)?;
        Ok(res.map(|df| self.build_from(df)))
    }
    /// [Float::from_i128_r](rustc_apfloat::Float::from_i128_r), `self` is ignored
    fn build_from_i128_r(&self, value: i128, round: Round) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        df.build_from_i128_r(value, round)
            .map(|from_i128_r| self.build_from(from_i128_r))
    }
    /// [Float::from_i128](rustc_apfloat::Float::from_i128), `self` is ignored
    fn build_from_i128(&self, value: i128) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        df.build_from_i128(value)
            .map(|from_i128| self.build_from(from_i128))
    }
    /// [Float::from_u128](rustc_apfloat::Float::from_u128), `self` is ignored
    fn build_from_u128(&self, value: u128) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        df.build_from_u128(value)
            .map(|from_u128| self.build_from(from_u128))
    }

    /// [Neg::neg](std::ops::Neg::neg)
    fn neg(&self) -> Box<dyn FloatAttr> {
        let df = self.get_inner();
        let negated = df.neg();
        self.build_from(negated)
    }
    /// [Add::add](std::ops::Add::add)
    fn add(&self, rhs: &dyn FloatAttr) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        let rhs_df = rhs.get_inner();
        df.add(rhs_df).map(|add| self.build_from(add))
    }
    /// [Sub::sub](std::ops::Sub::sub)
    fn sub(&self, rhs: &dyn FloatAttr) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        let rhs_df = rhs.get_inner();
        df.sub(rhs_df).map(|sub| self.build_from(sub))
    }
    /// [Mul::mul](std::ops::Mul::mul)
    fn mul(&self, rhs: &dyn FloatAttr) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        let rhs_df = rhs.get_inner();
        df.mul(rhs_df).map(|mul| self.build_from(mul))
    }
    /// [Div::div](std::ops::Div::div)
    fn div(&self, rhs: &dyn FloatAttr) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        let rhs_df = rhs.get_inner();
        df.div(rhs_df).map(|div| self.build_from(div))
    }
    /// [Rem::rem](std::ops::Rem::rem)
    fn rem(&self, rhs: &dyn FloatAttr) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        let rhs_df = rhs.get_inner();
        df.rem(rhs_df).map(|rem| self.build_from(rem))
    }
    /// [Float::add_r](rustc_apfloat::Float::add_r)
    fn add_r(&self, rhs: &dyn FloatAttr, round: Round) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        let rhs_df = rhs.get_inner();
        df.add_r(rhs_df, round).map(|add_r| self.build_from(add_r))
    }
    /// [Float::mul_r](rustc_apfloat::Float::mul_r)
    fn mul_r(&self, rhs: &dyn FloatAttr, round: Round) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        let rhs_df = rhs.get_inner();
        df.mul_r(rhs_df, round).map(|mul_r| self.build_from(mul_r))
    }
    /// [Float::mul_add_r](rustc_apfloat::Float::mul_add_r)
    fn mul_add_r(
        &self,
        rhs: &dyn FloatAttr,
        addend: &dyn FloatAttr,
        round: Round,
    ) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        let rhs_df = rhs.get_inner();
        let addend_df = addend.get_inner();
        df.mul_add_r(rhs_df, addend_df, round)
            .map(|mul_add_r| self.build_from(mul_add_r))
    }
    /// [Float::div_r](rustc_apfloat::Float::div_r)
    fn div_r(&self, rhs: &dyn FloatAttr, round: Round) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        let rhs_df = rhs.get_inner();
        df.div_r(rhs_df, round).map(|div_r| self.build_from(div_r))
    }
    /// [Float::ieee_rem](rustc_apfloat::Float::ieee_rem)
    fn ieee_rem(&self, rhs: &dyn FloatAttr) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        let rhs_df = rhs.get_inner();
        df.ieee_rem(rhs_df)
            .map(|ieee_rem| self.build_from(ieee_rem))
    }
    /// [Float::c_fmod](rustc_apfloat::Float::c_fmod)
    fn c_fmod(&self, rhs: &dyn FloatAttr) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        let rhs_df = rhs.get_inner();
        df.c_fmod(rhs_df).map(|c_fmod| self.build_from(c_fmod))
    }
    /// [Float::round_to_integral](rustc_apfloat::Float::round_to_integral)
    fn round_to_integral(&self, round: Round) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        df.round_to_integral(round)
            .map(|round_to_integral| self.build_from(round_to_integral))
    }
    /// [Float::next_up](rustc_apfloat::Float::next_up)
    fn next_up(&self) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        df.next_up().map(|next_up| self.build_from(next_up))
    }
    /// [Float::to_bits](rustc_apfloat::Float::to_bits)
    fn to_bits(&self) -> u128 {
        let df = self.get_inner();
        df.to_bits()
    }
    /// [Float::to_u128_r](rustc_apfloat::Float::to_u128_r)
    fn to_u128_r(&self, width: usize, round: Round, is_exact: &mut bool) -> StatusAnd<u128> {
        let df = self.get_inner();
        df.to_u128_r(width, round, is_exact)
    }
    /// [Float::cmp_abs_normal](rustc_apfloat::Float::cmp_abs_normal)
    fn cmp_abs_normal(&self, other: &dyn FloatAttr) -> Ordering {
        let df = self.get_inner();
        df.cmp_abs_normal(other.get_inner())
    }
    /// [Float::bitwise_eq](rustc_apfloat::Float::bitwise_eq)
    fn bitwise_eq(&self, other: &dyn FloatAttr) -> bool {
        let df = self.get_inner();
        df.bitwise_eq(other.get_inner())
    }
    /// [Float::is_negative](rustc_apfloat::Float::is_negative)
    fn is_negative(&self) -> bool {
        let df = self.get_inner();
        df.is_negative()
    }
    /// [Float::is_denormal](rustc_apfloat::Float::is_denormal)
    fn is_denormal(&self) -> bool {
        let df = self.get_inner();
        df.is_denormal()
    }
    /// [Float::is_signaling](rustc_apfloat::Float::is_signaling)
    fn is_signaling(&self) -> bool {
        let df = self.get_inner();
        df.is_signaling()
    }
    /// [Float::category](rustc_apfloat::Float::category)
    fn category(&self) -> Category {
        let df = self.get_inner();
        df.category()
    }
    /// [Float::get_exact_inverse](rustc_apfloat::Float::get_exact_inverse)
    fn get_exact_inverse(&self) -> Option<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        df.get_exact_inverse()
            .map(|inverse| self.build_from(inverse))
    }
    /// [Float::ilogb](rustc_apfloat::Float::ilogb)
    fn ilogb(&self) -> ExpInt {
        let df = self.get_inner();
        df.ilogb()
    }
    /// [Float::scalbn_r](rustc_apfloat::Float::scalbn_r)
    fn scalbn_r(&self, n: ExpInt, round: Round) -> Box<dyn FloatAttr> {
        let df = self.get_inner();
        self.build_from(df.scalbn_r(n, round))
    }
    /// [Float::frexp_r](rustc_apfloat::Float::frexp_r)
    fn frexp_r(&self, exp: &mut ExpInt, round: Round) -> Box<dyn FloatAttr> {
        let df = self.get_inner();
        let frexp_r = df.frexp_r(exp, round);
        self.build_from(frexp_r)
    }
    /// [Float::sub_r](rustc_apfloat::Float::sub_r)
    fn sub_r(&self, rhs: &dyn FloatAttr, round: Round) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        let rhs_df = rhs.get_inner();
        df.sub_r(rhs_df, round).map(|sub_r| self.build_from(sub_r))
    }
    /// [Float::mul_add](rustc_apfloat::Float::mul_add)
    fn mul_add(
        &self,
        multiplicand: &dyn FloatAttr,
        addend: &dyn FloatAttr,
    ) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        let multiplicand_df = multiplicand.get_inner();
        let addend_df = addend.get_inner();
        df.mul_add(multiplicand_df, addend_df)
            .map(|mul_add| self.build_from(mul_add))
    }
    /// [Float::next_down](rustc_apfloat::Float::next_down)
    fn next_down(&self) -> StatusAnd<Box<dyn FloatAttr>> {
        let df = self.get_inner();
        df.next_down().map(|next_down| self.build_from(next_down))
    }
    /// [Float::abs](rustc_apfloat::Float::abs)
    fn abs(&self) -> Box<dyn FloatAttr> {
        let df = self.get_inner();
        let abs = df.abs();
        self.build_from(abs)
    }
    /// [Float::copy_sign](rustc_apfloat::Float::copy_sign)
    fn copy_sign(&self, other: &dyn FloatAttr) -> Box<dyn FloatAttr> {
        let df = self.get_inner();
        let other_df = other.get_inner();
        self.build_from(df.copy_sign(other_df))
    }
    /// [Float::to_i128_r](rustc_apfloat::Float::to_i128_r)
    fn to_i128_r(&self, width: usize, round: Round, is_exact: &mut bool) -> StatusAnd<i128> {
        let df = self.get_inner();
        df.to_i128_r(width, round, is_exact)
    }
    /// [Float::to_i128](rustc_apfloat::Float::to_i128)
    fn to_i128(&self, width: usize) -> StatusAnd<i128> {
        let df = self.get_inner();
        df.to_i128(width)
    }
    /// [Float::to_u128](rustc_apfloat::Float::to_u128)
    fn to_u128(&self, width: usize) -> StatusAnd<u128> {
        let df = self.get_inner();
        df.to_u128(width)
    }
    /// [Float::min](rustc_apfloat::Float::min)
    fn min(&self, other: &dyn FloatAttr) -> Box<dyn FloatAttr> {
        let df = self.get_inner();
        let other_df = other.get_inner();
        self.build_from(df.min(other_df))
    }
    /// [Float::max](rustc_apfloat::Float::max)
    fn max(&self, other: &dyn FloatAttr) -> Box<dyn FloatAttr> {
        let df = self.get_inner();
        let other_df = other.get_inner();
        self.build_from(df.max(other_df))
    }
    /// [Float::minimum](rustc_apfloat::Float::minimum)
    fn minimum(&self, other: &dyn FloatAttr) -> Box<dyn FloatAttr> {
        let df = self.get_inner();
        let other_df = other.get_inner();
        self.build_from(df.minimum(other_df))
    }
    /// [Float::maximum](rustc_apfloat::Float::maximum)
    fn maximum(&self, other: &dyn FloatAttr) -> Box<dyn FloatAttr> {
        let df = self.get_inner();
        let other_df = other.get_inner();
        self.build_from(df.maximum(other_df))
    }
    /// [Float::is_normal](rustc_apfloat::Float::is_normal)
    fn is_normal(&self) -> bool {
        let df = self.get_inner();
        df.is_normal()
    }
    /// [Float::is_finite](rustc_apfloat::Float::is_finite)
    fn is_finite(&self) -> bool {
        let df = self.get_inner();
        df.is_finite()
    }
    /// [Float::is_zero](rustc_apfloat::Float::is_zero)
    fn is_zero(&self) -> bool {
        let df = self.get_inner();
        df.is_zero()
    }
    /// [Float::is_infinite](rustc_apfloat::Float::is_infinite)
    fn is_infinite(&self) -> bool {
        let df = self.get_inner();
        df.is_infinite()
    }
    /// [Float::is_nan](rustc_apfloat::Float::is_nan)
    fn is_nan(&self) -> bool {
        let df = self.get_inner();
        df.is_nan()
    }
    /// [Float::is_non_zero](rustc_apfloat::Float::is_non_zero)
    fn is_non_zero(&self) -> bool {
        let df = self.get_inner();
        df.is_non_zero()
    }
    /// [Float::is_finite_non_zero](rustc_apfloat::Float::is_finite_non_zero)
    fn is_finite_non_zero(&self) -> bool {
        let df = self.get_inner();
        df.is_finite_non_zero()
    }
    /// [Float::is_pos_zero](rustc_apfloat::Float::is_pos_zero)
    fn is_pos_zero(&self) -> bool {
        let df = self.get_inner();
        df.is_pos_zero()
    }
    /// [Float::is_neg_zero](rustc_apfloat::Float::is_neg_zero)
    fn is_neg_zero(&self) -> bool {
        let df = self.get_inner();
        df.is_neg_zero()
    }
    /// [Float::is_pos_infinity](rustc_apfloat::Float::is_pos_infinity)
    fn is_pos_infinity(&self) -> bool {
        let df = self.get_inner();
        df.is_pos_infinity()
    }
    /// [Float::is_neg_infinity](rustc_apfloat::Float::is_neg_infinity)
    fn is_neg_infinity(&self) -> bool {
        let df = self.get_inner();
        df.is_neg_infinity()
    }
    /// [Float::is_smallest](rustc_apfloat::Float::is_smallest)
    fn is_smallest(&self) -> bool {
        let df = self.get_inner();
        df.is_smallest()
    }
    /// [Float::is_smallest_normalized](rustc_apfloat::Float::is_smallest_normalized)
    fn is_smallest_normalized(&self) -> bool {
        let df = self.get_inner();
        df.is_smallest_normalized()
    }
    /// [Float::is_largest](rustc_apfloat::Float::is_largest)
    fn is_largest(&self) -> bool {
        let df = self.get_inner();
        df.is_largest()
    }
    /// [Float::is_integer](rustc_apfloat::Float::is_integer)
    fn is_integer(&self) -> bool {
        let df = self.get_inner();
        df.is_integer()
    }
    /// [Float::scalbn](rustc_apfloat::Float::scalbn)
    fn scalbn(&self, n: ExpInt) -> Box<dyn FloatAttr> {
        let df = self.get_inner();
        let scalbn = df.scalbn(n);
        self.build_from(scalbn)
    }
    /// [Float::frexp](rustc_apfloat::Float::frexp)
    fn frexp(&self, exp: &mut ExpInt) -> Box<dyn FloatAttr> {
        let df = self.get_inner();
        let frexp_r = df.frexp(exp);
        self.build_from(frexp_r)
    }

    fn verify(_attr: &dyn Attribute, _ctx: &Context) -> Result<()>
    where
        Self: Sized,
    {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use rustc_apfloat::ieee::Single;

    use super::*;
    use crate::builtin::attributes::FPSingleAttr;

    #[test]
    fn test_float_attr_give_build_qnan_neg() {
        let attr = FPSingleAttr(Single::from_str("1.0").unwrap());

        let qnan = attr.build_qnan(Some(42));
        assert!(qnan.get_inner().is_nan());

        let neg = attr.neg();
        assert!(
            (&*neg as &dyn Attribute)
                .downcast_ref::<FPSingleAttr>()
                .unwrap()
                != &attr
        );
        let neg_neg = neg.neg();
        assert!(
            (&*neg_neg as &dyn Attribute)
                .downcast_ref::<FPSingleAttr>()
                .is_some_and(|n| n == &attr)
        );
    }
}