nested_array_field_addr.mx raw

   1  // The address of an element of an array-of-arrays.
   2  //
   3  // stage4's emitIndexAddr decides whether the indexed base is an array and, when
   4  // it is, GEPs the array type with two indices. The detection looked only at the
   5  // index operand's own type, so a base that is a *pointer to* an array - a
   6  // struct field, a global, a *[N]E parameter - fell through to the branch that
   7  // treats the base as a pointer to one element, and emitted
   8  //
   9  //	getelementptr [8 x i32], ptr %row_of_field, i32 0, i32 %i
  10  //
  11  // which steps i*4 bytes instead of i*32: &h.q[1] pointed into h.q[0]. Indexing
  12  // an array value directly (a local `var q [n]row`) was fine because an alloca
  13  // carries its array type, which is why this survived.
  14  //
  15  // It was found by image/jpeg, whose decoder does
  16  // `qt := &d.quant[d.comp[compIndex].tq]` with `quant [4]block` and `block
  17  // [64]int32`: every chroma block was dequantized from the luma table read at a
  18  // 4-byte step, so decoded chroma came out wrong (a red 1x1 decoded to
  19  // 195,25,26 instead of 254,0,0) while the encoder stayed byte-identical to Go.
  20  // legacy builds this fixture correctly; the fix is stage4-only.
  21  package main
  22  
  23  import (
  24  	"os"
  25  )
  26  
  27  const n = 4
  28  const m = 8
  29  
  30  type row [m]int32
  31  
  32  type holder struct {
  33  	q [n]row
  34  }
  35  
  36  func check(name string, got, want int32) {
  37  	if got != want {
  38  		println("FAIL: " | name | ": got ", got, " want ", want)
  39  		os.Exit(1)
  40  	}
  41  }
  42  
  43  func checkArray(name string, r *row, base int32) {
  44  	for j := 0; j < m; j++ {
  45  		check(name, r[j], base|int32(j))
  46  	}
  47  }
  48  
  49  // checkParam indexes a pointer-to-array parameter.
  50  func checkParam(name string, p *[n]row) {
  51  	for i := 0; i < n; i++ {
  52  		r := &p[i]
  53  		checkArray(name, r, int32(i)*1000)
  54  	}
  55  }
  56  
  57  func main() {
  58  	// Termination: a batch program must still be stoppable, so the entry
  59  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  60  	select {
  61  	case <-shutdown:
  62  		return
  63  	default:
  64  	}
  65  
  66  	var h holder
  67  	for i := 0; i < n; i++ {
  68  		for j := 0; j < m; j++ {
  69  			h.q[i][j] = int32(i*1000 + j)
  70  		}
  71  	}
  72  
  73  	// Struct field: the address of a row.
  74  	for i := 0; i < n; i++ {
  75  		r := &h.q[i]
  76  		checkArray("field addr", r, int32(i)*1000)
  77  	}
  78  
  79  	// Through a pointer to the struct.
  80  	hp := &h
  81  	for i := 0; i < n; i++ {
  82  		r := &hp.q[i]
  83  		checkArray("ptr field addr", r, int32(i)*1000)
  84  	}
  85  
  86  	// A pointer-to-array parameter.
  87  	checkParam("param addr", &h.q)
  88  
  89  	// A pointer to the array loaded from a variable.
  90  	var pa *[n]row
  91  	pa = &h.q
  92  	for i := 0; i < n; i++ {
  93  		r := &pa[i]
  94  		checkArray("loaded ptr addr", r, int32(i)*1000)
  95  	}
  96  
  97  	// The plain local array keeps working.
  98  	var q [n]row
  99  	for i := 0; i < n; i++ {
 100  		for j := 0; j < m; j++ {
 101  			q[i][j] = int32(i*5000 + j)
 102  		}
 103  	}
 104  	for i := 0; i < n; i++ {
 105  		r := &q[i]
 106  		checkArray("local addr", r, int32(i)*5000)
 107  	}
 108  
 109  	// A slice of arrays addresses rows the same way.
 110  	s := []row{:n}
 111  	for i := 0; i < n; i++ {
 112  		for j := 0; j < m; j++ {
 113  			s[i][j] = int32(i*7000 + j)
 114  		}
 115  	}
 116  	for i := 0; i < n; i++ {
 117  		r := &s[i]
 118  		checkArray("slice addr", r, int32(i)*7000)
 119  	}
 120  
 121  	// Writing back through the row address must reach the row it names.
 122  	for i := 0; i < n; i++ {
 123  		r := &h.q[i]
 124  		r[0] = 9000 | int32(i)
 125  	}
 126  	for i := 0; i < n; i++ {
 127  		check("field row write", h.q[i][0], 9000|int32(i))
 128  	}
 129  
 130  	println("ok")
 131  }
 132