[plugins][ROCM] Fix minor loc source resize bug (#17133)

PR fixes minor resize source loc bug where we resize even if loc is
empty or unknown. This should fix
https://github.com/iree-org/iree/issues/17132
diff --git a/compiler/plugins/target/ROCM/ROCMTarget.cpp b/compiler/plugins/target/ROCM/ROCMTarget.cpp
index c2069a8..eb62b6c 100644
--- a/compiler/plugins/target/ROCM/ROCMTarget.cpp
+++ b/compiler/plugins/target/ROCM/ROCMTarget.cpp
@@ -566,10 +566,10 @@
 
       // Optional source location information for debugging/profiling.
       if (serOptions.debugLevel >= 1) {
-        // We only ever resize to the maximum -- so all previous data will
-        // be kept as-is.
-        sourceLocationRefs.resize(exportOps.size());
         if (auto loc = findFirstFileLoc(exportOp.getLoc())) {
+          // We only ever resize to the maximum -- so all previous data will
+          // be kept as-is.
+          sourceLocationRefs.resize(exportOps.size());
           auto filenameRef = builder.createString(loc->getFilename());
           sourceLocationRefs[ordinal] = iree_hal_rocm_FileLineLocDef_create(
               builder, filenameRef, loc->getLine());
diff --git a/compiler/plugins/target/ROCM/test/smoketest.mlir b/compiler/plugins/target/ROCM/test/smoketest.mlir
index 2a46244..12e81db 100644
--- a/compiler/plugins/target/ROCM/test/smoketest.mlir
+++ b/compiler/plugins/target/ROCM/test/smoketest.mlir
@@ -38,3 +38,30 @@
 //      CHECK:   hal.executable.binary public @rocm_hsaco_fb attributes {
 // CHECK-SAME:     data = dense
 // CHECK-SAME:     format = "rocm-hsaco-fb"
+
+// -----
+
+// Tests serialization with unknown source locations.
+
+#loc = loc(unknown)
+module attributes {
+  hal.device.targets = [
+    #hal.device.target<"rocm", [
+      #hal.executable.target<"rocm", "rocm-hsaco-fb">
+    ]>
+  ]
+} {
+
+stream.executable public @add_dispatch_0 {
+  stream.executable.export @add_dispatch_0 workgroups(%arg0 : index) -> (index, index, index) {
+    %x, %y, %z = flow.dispatch.workgroup_count_from_dag_root %arg0
+    stream.return %x, %y, %z : index, index, index
+  } loc(#loc)
+  builtin.module  {
+    func.func @add_dispatch_0() {
+      return
+    } loc(#loc)
+  } loc(#loc)
+} loc(#loc)
+
+}