The Knapp-Stein dimension theorem for $p$-adic groups