AnyDSL / MimIR

MimIR is my Intermediate Representation
https://anydsl.github.io/MimIR/
MIT License
46 stars 9 forks source link

Add mem adds too many mems #179

Open NeuralCoder3 opened 1 year ago

NeuralCoder3 commented 1 year ago

The add_mem pass adds unnecessary mems in nested argument tuples but not in the functions:

.con tup_pb_5577562 _5577572::[tup_s_5577574::[_5577575: .Idx 4294967296, _5577579: .Idx 4294967296], tup_ret_cont_5577584: .Cn [%mem.M, %mem.Ptr (.Idx 4294967296, 0)]] (readable: tup_pb: (Int*Int)*Cn (mem*Ptr(Int))) becomes [%mem.M, «2; .Idx 4294967296», .Cn [%mem.M, %mem.Ptr (.Idx 4294967296, 0)]] (mem*Int²*Cn(mem*Ptr(Int))) and the following error occurs:

<unknown location>: error: cannot pass argument 
  '(_2442813, _2442785, _2442643)' of type 
  '[%mem.M, [%mem.M, .Idx 4294967296, .Idx 4294967296], .Cn [%mem.M, %mem.Ptr (.Idx 4294967296, 0)]]' to 
  'tup_pb_5577562_reshape_2442834' of domain 
  '[%mem.M, «2; .Idx 4294967296», .Cn [%mem.M, %mem.Ptr (.Idx 4294967296, 0)]]'

The pass correctly adds the memory _2442813 in the outer tuple but also adds an additional memory in the inner tuple.

File: lit/mem/closure/out_of_nothing2_simpl.thorin in ad_ptr_merge.

NeuralCoder3 commented 1 year ago

Related issue:

File: lit/mem/closure/out_of_nothing2_simpl.thorin in ad_ptr_merge.

add_mem removes code by wrongly updating memory operations:

Original code:

.con q_5577173_reshape_2446227 _2446228::[_2446268: .Idx 4294967296, _2446230: .Cn [%mem.M, %mem.Ptr (.Idx 4294967296, 0)]] @(1:(.Idx 2)) = {
    .let _2446258: [%mem.M, %mem.Ptr (.Idx 4294967296, 0)] = %mem.malloc (.Idx 4294967296, 0) (⊤:%mem.M, 4);
    .let _2446275: %mem.M = %mem.store (.Idx 4294967296, 0) (_2446258#0:(.Idx 2), _2446258#1:(.Idx 2), _2446268);
    _2446230 (_2446275, _2446258#1:(.Idx 2))
};
.con printIntegerNL [%mem.M, .Idx 4294967296, .Cn %mem.M] @(<nullptr>) = {
     <unset>
};
.con .extern main __5577659_2446186::[mem_5577661_2446188: %mem.M, argc_5577675_2446203: .Idx 4294967296, %mem.Ptr (%mem.Ptr (.Idx 256, 0), 0), return_5577845_2446403: .Cn [%mem.M, .Idx 4294967296]] @(1:(.Idx 2)) = {
    .con __5577835_reshape_2446400 _2446404: %mem.M @(1:(.Idx 2)) = {
        return_5577845_2446403 (_2446404, 0:(.Idx 4294967296))
    };
    .con __5577811_reshape_2446395 _2446396: %mem.M @(1:(.Idx 2)) = {
        printIntegerNL (_2446396, ⊥:.Idx 4294967296, __5577835_reshape_2446400)
    };
    .con inner_cont_5577580_reshape_2446315 [%mem.M, %mem.Ptr (.Idx 4294967296, 0)] @(1:(.Idx 2)) = {
        .let _2446189: [%mem.M, %mem.Ptr (.Idx 4294967296, 0)] = %mem.alloc (.Idx 4294967296, 0) mem_5577661_2446188;
        .let _2446210: %mem.M = %mem.store (.Idx 4294967296, 0) (_2446189#0:(.Idx 2), _2446189#1:(.Idx 2), argc_5577675_2446203);
        .let _2446212: [%mem.M, %mem.Ptr (.Cn [.Idx 4294967296, .Cn [%mem.M, %mem.Ptr (.Idx 4294967296, 0)]], 0)] = %mem.malloc (.Cn [.Idx 4294967296, .Cn [%mem.M, %mem.Ptr (.Idx 4294967296, 0)]], 0) (_2446210, 8);
        .let _2446284: %mem.M = %mem.store (.Cn [.Idx 4294967296, .Cn [%mem.M, %mem.Ptr (.Idx 4294967296, 0)]], 0) (_2446212#0:(.Idx 2), _2446212#1:(.Idx 2), q_5577173_reshape_2446227);
         //  ^ this store is dropped
        .let _2446292: [%mem.M, .Idx 4294967296] = %mem.load (.Idx 4294967296, 0) (_2446284, _2446189#1:(.Idx 2));
        .let _2446392: .Idx 4294967296 = %core.wrap.mul 4294967296 0 (2:(.Idx 4294967296), _2446292#1:(.Idx 2));
        printIntegerNL (⊤:%mem.M, _2446392, __5577811_reshape_2446395)
    };
    .let _2446302: [%mem.M, .Cn [.Idx 4294967296, .Cn [%mem.M, %mem.Ptr (.Idx 4294967296, 0)]]] = %mem.load (.Cn [.Idx 4294967296, .Cn [%mem.M, %mem.Ptr (.Idx 4294967296, 0)]], 0) (_2446292#0:(.Idx 2), _2446212#1:(.Idx 2));
    _2446302#1:(.Idx 2) (_2446292#1:(.Idx 2), inner_cont_5577580_reshape_2446315)
};

turns into

.con .extern main __5577659_2446447::[mem_5577661_2446456: %mem.M, argc_5577675_2446466: .Idx 4294967296, _2446481: %mem.Ptr (%mem.Ptr (.Idx 256, 0), 0), return_5577845_2446491: .Cn [%mem.M, .Idx 4294967296]] @(1:(.Idx 2)) = {
    .con __5577835_reshape_2446888 _2446892: %mem.M @(1:(.Idx 2)) = {
        return_5577845_2446491 (_2446892, 0:(.Idx 4294967296))
    };
    .con __5577811_reshape_2446857 _2446861: %mem.M @(1:(.Idx 2)) = {
        printIntegerNL (_2446861, ⊥:.Idx 4294967296, __5577835_reshape_2446888)
    };
    .con inner_cont_5577580_reshape_2446796 _2446801::[_2446815: %mem.M, _2446830: %mem.Ptr (.Idx 4294967296, 0)] @(1:(.Idx 2)) = {
        .let _2446532: [%mem.M, %mem.Ptr (.Idx 4294967296, 0)] = %mem.alloc (.Idx 4294967296, 0) mem_5577661_2446456;
        .let _2446728: [%mem.M, .Idx 4294967296] = %mem.load (.Idx 4294967296, 0) (mem_5577661_2446456, _2446532#1:(.Idx 2));
        .let _2446979: .Idx 4294967296 = %core.wrap.mul 4294967296 0 (2:(.Idx 4294967296), _2446728#1:(.Idx 2));
        printIntegerNL (_2446815, _2446979, __5577811_reshape_2446857)
    };
    .let _2446627: [%mem.M, %mem.Ptr (.Cn [%mem.M, .Idx 4294967296, .Cn [%mem.M, %mem.Ptr (.Idx 4294967296, 0)]], 0)] = %mem.malloc (.Cn [%mem.M, .Idx 4294967296, .Cn [%mem.M, %mem.Ptr (.Idx 4294967296, 0)]], 0) (mem_5577661_2446456, 8);
    .let _2446772: [%mem.M, .Cn [%mem.M, .Idx 4294967296, .Cn [%mem.M, %mem.Ptr (.Idx 4294967296, 0)]]] = %mem.load (.Cn [%mem.M, .Idx 4294967296, .Cn [%mem.M, %mem.Ptr (.Idx 4294967296, 0)]], 0) (mem_5577661_2446456, _2446627#1:(.Idx 2));
    _2446772#1:(.Idx 2) (mem_5577661_2446456, _2446728#1:(.Idx 2), inner_cont_5577580_reshape_2446796)
};

The higher-order store for instance is dropped during the transformation.

NeuralCoder3 commented 1 year ago

A further (possibly simpler) example: lit/matrix/print_const_dyn_mat.thorin in NeuralCoder3/thorin2/matrix_dialect Command: ./build/bin/thorin -d direct -d affine -d math -d matrix -d clos lit/matrix/print_const_dyn_mat.thorin -o - -VVVV

An additional mem is inserted in the bitcast call from print_int_matrix_wrap which is inlined in f (happens during second reshape to f_reshape_reshape).