Skip to content

[clang++][instruction re-order][performance bug] #158219

@xiaoqiang-wang

Description

@xiaoqiang-wang

I‘m using clang++ to compile my below C++ code,

static const INSTR_SIZE  =  64;
uint64_t enc;
uint32_t temp;


//void __attribute__((optimize("-fno-reorder-blocks-and-partition")))  set_field(uint8_t pos, uint8_t size, instr_size_t val)

//void __attribute__((optimize("-fno-reorder-blocks")))  set_field(uint8_t pos, uint8_t size, instr_size_t val) 

void  set_field(uint8_t pos, uint8_t size, instr_size_t val) {

  instr_size_t mask = ((1ULL << size) - 1);

  assert(((val & mask) == val) && "Value too wide for field");

  if (pos < INSTR_SIZE) {
    assert(pos + size <= INSTR_SIZE);

    enc &= ~(mask << pos);
    enc |= (val << pos);
  }
  else {
    assert(pos + size <= INSTR_SIZE + 32);

    pos &= (INSTR_SIZE - 1);
    temp &= ~(mask << pos);
    temp |= (val << pos);
  }
}

but there is a performance bug caused by instruction re-order or instruction-combine.

I want the code first try go into the if-block, then go to else-block.
but since of const-folding and instruction-reorder,
the clang++ make code work as below order:

temp &=~(mask<<63)
if(pos < 64){
...
}
else{
...
}

I do not want calculate temp &=~(mask<<63) first.


I tried __attribute__((optimize("-fno-reorder-blocks"))) , want to disable instruction-reorder, but it seems not work.

Q1: Is there any way to disable instruction-reorder for this function?


below is my machine info

user_name: work_dir $ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.5 LTS
Release:	22.04
Codename:	jammy
user_name: work_dir $ llvm-readelf -p .comment my_project_exe

String dump of section '.comment':
[     0] Linker: LLD 20.0.0 (/mnt/disks/build-disk/src/android/llvm-r547379-release/out/llvm-project/llvm b718bcaf8c198c82f3021447d943401e3ab5bd54)
[    8b] GCC: (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3
[    b5] GCC: (GNU) 4.8.3
[    c7] Android (13290119, +pgo, +bolt, +lto, +mlgo, based on r547379) clang version 20.0.0 (https://android.googlesource.com/toolchain/llvm-project b718bcaf8c198c82f3021447d943401e3ab5bd54)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions