}
bool
-is_scratch_offset_valid(opt_ctx& ctx, Instruction* instr, int32_t offset)
+is_scratch_offset_valid(opt_ctx& ctx, Instruction* instr, int64_t offset0, int64_t offset1)
{
bool negative_unaligned_scratch_offset_bug = ctx.program->gfx_level == GFX10;
int32_t min = ctx.program->dev.scratch_global_offset_min;
int32_t max = ctx.program->dev.scratch_global_offset_max;
+ int64_t offset = offset0 + offset1;
+
bool has_vgpr_offset = instr && !instr->operands[0].isUndefined();
if (negative_unaligned_scratch_offset_bug && has_vgpr_offset && offset < 0 && offset % 4)
return false;
while (info.is_temp())
info = ctx.info[info.temp.id()];
- if (i <= 1 && parse_base_offset(ctx, instr.get(), i, &base, &offset, false) &&
+ /* The hardware probably does: 'scratch_base + u2u64(saddr) + i2i64(offset)'. This means
+ * we can't combine the addition if the unsigned addition overflows and offset is
+ * positive. In theory, there is also issues if
+ * 'ilt(offset, 0) && ige(saddr, 0) && ilt(saddr + offset, 0)', but that just
+ * replaces an already out-of-bounds access with a larger one since 'saddr + offset'
+ * would be larger than INT32_MAX.
+ */
+ if (i <= 1 && parse_base_offset(ctx, instr.get(), i, &base, &offset, true) &&
base.regClass() == instr->operands[i].regClass() &&
- is_scratch_offset_valid(ctx, instr.get(), scratch.offset + (int32_t)offset)) {
+ is_scratch_offset_valid(ctx, instr.get(), scratch.offset, (int32_t)offset)) {
+ instr->operands[i].setTemp(base);
+ scratch.offset += (int32_t)offset;
+ continue;
+ } else if (i <= 1 && parse_base_offset(ctx, instr.get(), i, &base, &offset, false) &&
+ base.regClass() == instr->operands[i].regClass() && (int32_t)offset < 0 &&
+ is_scratch_offset_valid(ctx, instr.get(), scratch.offset, (int32_t)offset)) {
instr->operands[i].setTemp(base);
scratch.offset += (int32_t)offset;
continue;
} else if (i <= 1 && info.is_constant_or_literal(32) &&
ctx.program->gfx_level >= GFX10_3 &&
- is_scratch_offset_valid(ctx, NULL, scratch.offset + (int32_t)info.val)) {
+ is_scratch_offset_valid(ctx, NULL, scratch.offset, (int32_t)info.val)) {
/* GFX10.3+ can disable both SADDR and ADDR. */
instr->operands[i] = Operand(instr->operands[i].regClass());
scratch.offset += (int32_t)info.val;