This was originally ported from rocm device libs in 0ab07e1bde7d002f1a4c30babb6241c0cc366320. Merge in more recent changes.